473,836 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::string operator< ?

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::filesyst em::path& lhs,
const boost::filesyst em::path& rhs) {
std::string path1 = lhs.leaf();
std::string path2 = rhs.leaf();
boost::algorith m::to_lower( path1 );
boost::algorith m::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_stri ng
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
4 2254
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_stri ng
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_stri ng::compare.
But Meyers doesn't mention this function either. So question #2 is still
valid :)

--
Regards,
Matthias
Jul 23 '05 #2
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_stri ng
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_stri ng::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<<lexicogra phical_compare( s1.begin(), s1.end(), s2.begin(),
s2.end())
<<endl;
}


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #3
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<<lexicogra phical_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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2533
by: gbgbgbgb | last post by:
Hi, I have a definition bool operator<(string s_s, string s_t) { .... } and a variable list<string> concomp;
5
5018
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, std::char_traits<char> >& << Thing&' operator friendop.cpp:27: candidates are: std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>&, const Thing&) friendop.cpp:14: std::basic_ostream<_CharT, _Traits>&...
19
6195
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 found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
14
2347
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> using namespace std;
24
17495
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 managed C++. It seems to work fine, but everyone knows that programs with errors can still appear to "work fine" :) I am working with VS .NET 2003; I am unable to upgrade to 2005 at this time, so I cannot use the newer syntax or features. ...
5
2594
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 ); I then use a set of defines, e.g.:
29
23238
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 write to the file: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) I don't know what I am doing wrong. I have posted my entire program
2
1475
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 confused as to why the operator << overloading seems to be selecting the wrong function. If you look at the example program below, it should print this: The string is 'Five == 5'
3
4282
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 be called. However, I am using the boost::format library that internally makes use of a string stream to store the formatted string. There:
0
9827
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9677
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10862
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10560
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10607
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6986
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.