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 4 2146
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: gbgbgbgb |
last post by:
Hi,
I have a definition
bool operator<(string s_s, string s_t)
{
....
}
and a variable
list<string> concomp;
|
by: Gianni Mariani |
last post by:
Can anyone enligten me why I get the "ambiguous overload" error from the
code below:
friendop.cpp: In function `int main()':
friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,...
|
by: Erik Wikström |
last post by:
First of all, forgive me if this is the wrong place to ask this question,
if it's a stupid question (it's my second week with C++), or if this is
answered some place else (I've searched but not...
|
by: lutorm |
last post by:
Hi everyone,
I'm trying to use istream_iterators to read a file consisting of pairs
of numbers. To do this, I wrote the following:
#include <fstream>
#include <vector>
#include <iterator>
...
|
by: Marcus Kwok |
last post by:
Hello, I am working on cleaning up some code that I inherited and was
wondering if there is anything wrong with my function. I am fairly
proficient in standard C++ but I am pretty new to the .NET...
|
by: probstm |
last post by:
I am using a message handler class that implements amongst others:
static void message ( std::string aHeader,
std::string aMessage,
int aTimeout = 0,
MessageType aFlag =
MSG_Information );
...
|
by: aarthi28 |
last post by:
Hi,
I have written this code, and at the end, I am trying to write a
vector of strings into a text file. However, my program is nor
compiling, and it gives me the following error when I try to...
|
by: Adam Nielsen |
last post by:
Hi everyone,
Following advice previously given in this group, I've created a function
that I'm using to "inline" the creation of a string. Regardless of what
you think of my method, I'm...
|
by: Martin T. |
last post by:
Hello.
I tried to overload the operator<< for implicit printing of wchar_t
string on a char stream.
Normally using it on a ostream will succeed as
std::operator<<<std::char_traits<char>
will...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: DizelArs |
last post by:
Hi all)
Faced with a problem, element.click() event doesn't work in Safari browser.
Tried various tricks like emulating touch event through a function:
let clickEvent = new Event('click', {...
| |