Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using std::find() on std::string vectors

20 views
Skip to first unread message

Lim Bio Liong

unread,
Nov 16, 2009, 2:03:01 AM11/16/09
to
Hello All,

I have a question on the use of std::find() on a vector that contains
std::string objects.

When I need to do a search on a vector of strings, I currently use the
std::find() algorithm, e.g. :

std::vector<std::string>::iterator theIterator;

theIterator = std::find
(
vecStrings.begin(),
vecStrings.end(),
strToMatch
);

I then declare a global == operator :

bool operator == (const std::string& lhs, const std::stringr& rhs)
{
return (strcmp(lhs.c_str(), rhs.c_str()) == 0);
}

This works. But I need to know if this is the standard way of performing a
std::find() on a vector of std::strings.

Thanks, all,
- Bio.


Alex Blekhman

unread,
Nov 16, 2009, 2:50:36 AM11/16/09
to
"Lim Bio Liong" wrote:
> [...]

> I then declare a global == operator :
>
> bool operator == (const std::string& lhs, const std::stringr&
> rhs)
> {
> return (strcmp(lhs.c_str(), rhs.c_str()) == 0);
> }
>
> This works. But I need to know if this is the standard way of
> performing a std::find() on a vector of std::strings.

You don't need to define `operator ==' for `std::string'. This
operator is already defined in <string> header. Otherwise, your
code is fine.

Alex

Lim Bio Liong

unread,
Nov 16, 2009, 6:45:01 AM11/16/09
to
Thanks Alex.

Stephan T. Lavavej [MSFT]

unread,
Nov 16, 2009, 3:23:45 PM11/16/09
to
If you're able to use std::string, but not its helper operators, then 99.99%
of the time you've forgotten to include <string>. The way VC's
implementation works, std::string is defined in an internal header (named
<xstring>; NEVER EVER include our internal headers directly), and its helper
operators are defined in the public header <string>. Some of our headers
which need std::string but not its helper operators include <xstring>
without including <string>. So, if you've forgotten to include <string>,
but you've happened to drag in <xstring>, std::string will be usable but its
helper operators will be mysteriously missing.

As the Standard headers include each other in unspecified ways, you should
always be careful to include the headers that you need.

Stephan T. Lavavej
Visual C++ Libraries Developer

"Alex Blekhman" <tkfx....@yahoo.com> wrote in message
news:uN1M9FpZ...@TK2MSFTNGP05.phx.gbl...

0 new messages