By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

std::string operator< ?

P: n/a
Now I am confused.
I am doing a "less" comparison in my program between strings, to see
which strings in some range precede others.
This is my approach:

class SortedByName
{
public:
bool operator() (const boost::filesystem::path& lhs,
const boost::filesystem::path& rhs) {
std::string path1 = lhs.leaf();
std::string path2 = rhs.leaf();
boost::algorithm::to_lower( path1 );
boost::algorithm::to_lower( path2 );
return path1 < path2;
}
};

Now, there are at least two questions coming to my mind:

1. How does operator< work on strings? I wanted to look it up in my C++
implementation doc, but it doesn't exist in the std::basic_string
reference! (the comparison works by the way).

2. Meyers has an own item in his STL book which is dedicated to exactly
this topic. He doesn't even mention an operator< to do the comparison,
but suggests to use lexicographical_compare, which however would need me
to supply a (correct and portable) comparison predicate for characters.

What's the deal?

--
Regards,
Matthias
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Matthias wrote:
1. How does operator< work on strings? I wanted to look it up in my C++
implementation doc, but it doesn't exist in the std::basic_string
reference! (the comparison works by the way).


I found the reason why it was not listed. The global operator< will be
used, and it will invoke std::basic_string::compare.
But Meyers doesn't mention this function either. So question #2 is still
valid :)

--
Regards,
Matthias
Jul 23 '05 #2

P: n/a
Matthias wrote:

Matthias wrote:
1. How does operator< work on strings? I wanted to look it up in my
C++ implementation doc, but it doesn't exist in the std::basic_string
reference! (the comparison works by the way).

I found the reason why it was not listed. The global operator< will be
used, and it will invoke std::basic_string::compare.
But Meyers doesn't mention this function either. So question #2 is still
valid :)

The two comparisons below are equivalent. No predicate is required for char.
#include <string>
#include <iostream>
#include <ostream>
#include <algorithm>
int main()
{
using namespace std;

string s1="string1";

string s2="string2";

cout<<(s1<s2)<<endl;

cout<<lexicographical_compare(s1.begin(), s1.end(), s2.begin(),
s2.end())
<<endl;
}


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #3

P: n/a
Ioannis Vranos wrote:
The two comparisons below are equivalent. No predicate is required for
char.
#include <string>
#include <iostream>
#include <ostream>
#include <algorithm>
int main()
{
using namespace std;

string s1="string1";

string s2="string2";

cout<<(s1<s2)<<endl;

cout<<lexicographical_compare(s1.begin(), s1.end(), s2.begin(),
s2.end())
<<endl;
}


And what comparison function does lexicographical_compare use by
default? Is it portable? I'm just wondering why Meyers would encourage
you to write your own, if it isn't necessary.

--
Regards,
Matthias
Jul 23 '05 #4

P: n/a
Matthias wrote:
And what comparison function does lexicographical_compare use by
default? Is it portable? I'm just wondering why Meyers would encourage
you to write your own, if it isn't necessary.

It is explained nicely here:

http://msdn.microsoft.com/library/de...calcompare.asp


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.