473,657 Members | 2,507 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using less<string>...

Hi,

- - - - - - - - - - - - - - -
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
string name;
int pop;

string states[] = { "Wyoming", "Colorado", "Nevada",
"Montana", "Arizona", "Idaho"};
int pops[] = { 470, 2890, 800, 787, 2718, 944 };
map<string, int, less<string> > mapStates;

// Try replacing the above with:
// map<string, int> mapStates;
map<string, int, less<string> >::iterator iter; //iterator

for(int j=0; j<6; j++)
{
name = states[j]; //get data from arrays
pop = pops[j];
mapStates[name] = pop; //put it in map
}
cout << "Enter state: "; //get state from user

cin >> name;
pop = mapStates[name]; //find population
cout << "Population : " << pop << ",000\n";

cout << endl; //display entire map
for(iter = mapStates.begin (); iter != mapStates.end() ; iter++)
cout << (*iter).first << ' ' << (*iter).second << ",000\n";
return 0;
}

- - - - - - - - - - - - - - -

The question is: I don't see any difference between:
map<string, int, less<string> > mapStates;

and

map<string, int> mapStates;

I tried to google around a bit and found that both lines call the map
constructor to create a map<string, int> and the less<string> is a
comparison function - a 'weak ordering'. Can I see how less<string> is
defined somewhere?

Why don't I see any difference between both methods? Is less<string>
default? Is there also a more<string>-function?

So I also tried to look for it at www.cppreference.com which has some
nice descriptions but I couldn't find it...

Can anyone clarify this issue to me?
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 13 '06 #1
11 8778
Martin Jørgensen wrote:
Hi,

- - - - - - - - - - - - - - -
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
string name;
int pop;

string states[] = { "Wyoming", "Colorado", "Nevada",
"Montana", "Arizona", "Idaho"};
int pops[] = { 470, 2890, 800, 787, 2718, 944 };
map<string, int, less<string> > mapStates;

// Try replacing the above with:
// map<string, int> mapStates;
map<string, int, less<string> >::iterator iter; //iterator

for(int j=0; j<6; j++)
{
name = states[j]; //get data from arrays
pop = pops[j];
mapStates[name] = pop; //put it in map
}
cout << "Enter state: "; //get state from user
cin >> name;
pop = mapStates[name]; //find population
cout << "Population : " << pop << ",000\n";

cout << endl; //display entire map
for(iter = mapStates.begin (); iter != mapStates.end() ; iter++)
cout << (*iter).first << ' ' << (*iter).second << ",000\n";
return 0;
}

- - - - - - - - - - - - - - -

The question is: I don't see any difference between:
map<string, int, less<string> > mapStates;

and

map<string, int> mapStates;
There is no difference. The default Comparison for a map is
std::less<Key> so you've simply explicitly provided the default parameter.

I tried to google around a bit and found that both lines call the map
constructor to create a map<string, int> and the less<string> is a
comparison function - a 'weak ordering'. Can I see how less<string> is
defined somewhere?


You can look at your implementation' s definition although in the end
you'll find it's equivalent to operator<.

-Mark
May 13 '06 #2
Martin Jørgensen wrote:
[..]
The question is: I don't see any difference between:
map<string, int, less<string> > mapStates;

and

map<string, int> mapStates;
There isn't any (except for more typing in the former case).
I tried to google around a bit and found that both lines call the map
constructor to create a map<string, int> and the less<string> is a
comparison function - a 'weak ordering'. Can I see how less<string> is
defined somewhere?
It's defined in <functional> header.
Why don't I see any difference between both methods? Is less<string>
default?
Yes.
Is there also a more<string>-function?
No, not 'more<string>'. It's called greater<string> .
So I also tried to look for it at www.cppreference.com which has some
nice descriptions but I couldn't find it...

Can anyone clarify this issue to me?

A good book on Standard Library can. Nicolai Josuttis published a very
good one.

V
--
Please remove capital As from my address when replying by mail
May 13 '06 #3
Mark P wrote:
Martin Jørgensen wrote: -snip-
There is no difference. The default Comparison for a map is
std::less<Key> so you've simply explicitly provided the default parameter.


I thought so.
I tried to google around a bit and found that both lines call the map
constructor to create a map<string, int> and the less<string> is a
comparison function - a 'weak ordering'. Can I see how less<string> is
defined somewhere?

You can look at your implementation' s definition although in the end
you'll find it's equivalent to operator<.


I use g++ on one computer and MS Visual studio 2005 on another computer.

Where is my implementation' s definitions?
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 13 '06 #4
Martin Jørgensen wrote:
Mark P wrote:
Martin Jørgensen wrote:

-snip-
There is no difference. The default Comparison for a map is
std::less<Key> so you've simply explicitly provided the default
parameter.


I thought so.
I tried to google around a bit and found that both lines call the map
constructor to create a map<string, int> and the less<string> is a
comparison function - a 'weak ordering'. Can I see how less<string>
is defined somewhere?

You can look at your implementation' s definition although in the end
you'll find it's equivalent to operator<.


I use g++ on one computer and MS Visual studio 2005 on another computer.

Where is my implementation' s definitions?


No idea. Look for it. On Linux you might start in /usr/include.
Probably a file which includes functional in its name, so start with
"man find".

May 13 '06 #5
Victor Bazarov wrote:
Martin Jørgensen wrote:

-snip-
I tried to google around a bit and found that both lines call the map
constructor to create a map<string, int> and the less<string> is a
comparison function - a 'weak ordering'. Can I see how less<string> is
defined somewhere?

It's defined in <functional> header.


I don't understand that. I didn't include any <functional> header.
What's the definition of less<some type>?
Why don't I see any difference between both methods? Is less<string>
default?

Yes.

Is there also a more<string>-function?

No, not 'more<string>'. It's called greater<string> .


Ok.
So I also tried to look for it at www.cppreference.com which has some
nice descriptions but I couldn't find it...

Can anyone clarify this issue to me?


A good book on Standard Library can. Nicolai Josuttis published a very
good one.


I don't think I need a third book for such a simple question. I would
guess that the definition of less is something like:

bool less(char *first, char *second)
{
int i;
bool lesser_than = 0;

for(i=0; i<strlen(...); i++)
{
// compare all letters
if(something)
lesser_than = 1;
}

return lesser_than;
}

But I guess I can see it somewhere, can't I? Perhaps it's slightly
off-topic if it depends on my operating system but suppose I use both
g++ on mac/linux and visual studio 2005. Isn't those definitions inside
some \include\-directory ?

Or should I ask in another group?
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 13 '06 #6
Martin Jørgensen wrote:
Victor Bazarov wrote:
Martin Jørgensen wrote: -snip-
I tried to google around a bit and found that both lines call the
map constructor to create a map<string, int> and the less<string>
is a comparison function - a 'weak ordering'. Can I see how
less<string> is defined somewhere?

It's defined in <functional> header.


I don't understand that. I didn't include any <functional> header.


So? You did include <map>, didn't you? Look there, it's very likely
if they have to use 'less<>', they included <functional> (or some other
header that defines 'less<>' template. The Standard says that if *you*
want to use 'less<>', you should include <functional>. That's good
enough reason for me to start looking there.
What's the definition of less<some type>?
The Standard says that it's

template <class T> struct less : binary_function <T,T,bool> {
bool operator()(cons t T& x, const T& y) const;
};

and that the operator() returns x < y. What's beyond that? Well, here
is what I know: <string> header defines operator< for 'std::basic_str ing'
which in turn calls the 'compare' member of 'std::basic_str ing'. How
*it* does the comparison is not defined, only what the result should be.
So, you're still have to look at the library source if it's availabe to
you. Or in the book.
Why don't I see any difference between both methods? Is less<string>
default?

Yes.

Is there also a more<string>-function?

No, not 'more<string>'. It's called greater<string> .


Ok.
So I also tried to look for it at www.cppreference.com which has
some nice descriptions but I couldn't find it...

Can anyone clarify this issue to me?


A good book on Standard Library can. Nicolai Josuttis published a
very good one.


I don't think I need a third book for such a simple question.


Oh, you already have two books? That should be plenty. There was an old
Russian joke when two policemen were trying to decide what to buy one of
their colleagues as a birthday present. One says, "Let's buy him a book".
The other one shakes his head and says, "Nah, he's already got one".
I would
guess that the definition of less is something like:

bool less(char *first, char *second)
{
int i;
bool lesser_than = 0;

for(i=0; i<strlen(...); i++)
{
// compare all letters
if(something)
lesser_than = 1;
}

return lesser_than;
}

But I guess I can see it somewhere, can't I?
If your standard library comes with the source code, you could look
there. If it doesn't, we can't help. Every implemntation is
different and there is no guarantee that if you find one somewhere,
the one your compiler uses would be the same.
Perhaps it's slightly
off-topic if it depends on my operating system but suppose I use both
g++ on mac/linux and visual studio 2005. Isn't those definitions
inside some \include\-directory ?
Possible. Again, it's only so if your library comes in source code.
Or should I ask in another group?


If you need some information for a particular compiler you should
ask in the newsgroup dedicated to that compiler. Here we can only
give you general C++ answers, information on how to do things portably
and what the Standard says about things in the language or the library
(but usually not how they are implemented, that's up to the compiler
and/or library vendors).

V
--
Please remove capital As from my address when replying by mail
May 13 '06 #7
Mark P wrote:
-snip-
You can look at your implementation' s definition although in the end
you'll find it's equivalent to operator<.

I use g++ on one computer and MS Visual studio 2005 on another computer.

Where is my implementation' s definitions?


No idea. Look for it. On Linux you might start in /usr/include.
Probably a file which includes functional in its name, so start with
"man find".


That's easy for you to say...

find /usr/include | xargs grep -l 'less'

Gives a HUGE amount of files...
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 13 '06 #8
Victor Bazarov wrote:
Martin Jørgensen wrote: -snip-
I don't understand that. I didn't include any <functional> header.

So? You did include <map>, didn't you? Look there, it's very likely
if they have to use 'less<>', they included <functional> (or some other
header that defines 'less<>' template. The Standard says that if *you*
want to use 'less<>', you should include <functional>. That's good
enough reason for me to start looking there.


Hmm. I tried, but there are too many files :-(
What's the definition of less<some type>?

The Standard says that it's

template <class T> struct less : binary_function <T,T,bool> {
bool operator()(cons t T& x, const T& y) const;
};


Ok.
and that the operator() returns x < y. What's beyond that? Well, here
is what I know: <string> header defines operator< for 'std::basic_str ing'
which in turn calls the 'compare' member of 'std::basic_str ing'. How
*it* does the comparison is not defined, only what the result should be.
So, you're still have to look at the library source if it's availabe to
you. Or in the book.


The book = the standard you mean?
I don't think I need a third book for such a simple question.

Oh, you already have two books? That should be plenty. There was an old
Russian joke when two policemen were trying to decide what to buy one of
their colleagues as a birthday present. One says, "Let's buy him a book".
The other one shakes his head and says, "Nah, he's already got one".


If you're a professional programmer I guess you have plenty of C++
books. If you're not, you don't have so many. It all depends on what you
mostly spend your day-time on doing.
I would
guess that the definition of less is something like:

bool less(char *first, char *second)
{
int i;
bool lesser_than = 0;

for(i=0; i<strlen(...); i++)
{
// compare all letters
if(something)
lesser_than = 1;
}

return lesser_than;
}

But I guess I can see it somewhere, can't I?

If your standard library comes with the source code, you could look
there. If it doesn't, we can't help. Every implemntation is
different and there is no guarantee that if you find one somewhere,
the one your compiler uses would be the same.


Okay. Fine enough. I basically found out it was just "<" and my book
didn't explain that other than "it is the function that determines the
ordering of the set/map".
Perhaps it's slightly
off-topic if it depends on my operating system but suppose I use both
g++ on mac/linux and visual studio 2005. Isn't those definitions
inside some \include\-directory ?

Possible. Again, it's only so if your library comes in source code.


Looks too complicated for me - I have tried to look in some
include-directories and tried to search for different patterns :-)
Or should I ask in another group?

If you need some information for a particular compiler you should
ask in the newsgroup dedicated to that compiler. Here we can only
give you general C++ answers, information on how to do things portably
and what the Standard says about things in the language or the library
(but usually not how they are implemented, that's up to the compiler
and/or library vendors).


Why do they make so many strange #defines __Blablabla ? It's really
confusing....
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 13 '06 #9
Martin Jørgensen wrote:
Mark P wrote:
-snip-
You can look at your implementation' s definition although in the end
you'll find it's equivalent to operator<.
I use g++ on one computer and MS Visual studio 2005 on another computer.

Where is my implementation' s definitions?


No idea. Look for it. On Linux you might start in /usr/include.
Probably a file which includes functional in its name, so start with
"man find".


That's easy for you to say...

find /usr/include | xargs grep -l 'less'

Gives a HUGE amount of files...
Best regards / Med venlig hilsen
Martin Jørgensen


USUALLY, you will find the g++ std headers in

/usr/include/c++/<version>/

Mine is

/usr/include/c++/3.3.5/

If you look at

/usr/include/c++/3.3.5/functional

you may find something like this

#ifndef _CPP_FUNCTIONAL
#define _CPP_FUNCTIONAL 1

#pragma GCC system_header
#include <bits/c++config.h>
#include <cstddef>
#include <bits/stl_function.h>

#endif /* _CPP_FUNCTIONAL */

Then you would look in

/usr/include/c++/3.3.5/bits/stl_function.h

Where you might find

struct less : public binary_function <_Tp,_Tp,bool >
{
bool operator()(cons t _Tp& __x, const _Tp& __y) const
{ return __x < __y; }
};

So, 'less' depends on operator '<' for type '_Tp'.

Operator '<' is defined for strings.
As an excercise, you might try to find it...

It's been a few years, but I believe the use of
operator '<' by less is in the docs; something
like this (from the MSDN on-line docs):

"template<c lass T>
struct less : public binary_function <T, T, bool> {
bool operator()(cons t T& x, const T& y) const;
};
The template class defines its member function as
returning x < y. The member function defines a total
ordering, even if T is an object pointer type."

ref:
http://msdn.microsoft.com/library/de...ML/stdlbhm.asp

Regards,
Larry

May 13 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
5056
by: Gaurav | last post by:
Hello I have a program that basically inverts the contents of files except first line. It compiles fine but gives me core dump on running. If i comment temp.clear() it runs fine, but i need it to clear the temp vector for each file. ********************* code *******************
1
17949
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an application using C++ and the STL for handling my data. Unfortunately, I must interact with a (vanilla) C API. I use vectors of strings (for simplicity and less memory hassle), but the function calls for this API require arrays of character...
37
7340
by: Zombie | last post by:
Hi, what is the correct way of converting contents of a <string> to lowercase? There are no methods of <string> class to do this so I fallback on strlwr(). But the c_str() method returns a const pointer which cannot be used with strlwr() as it does the conversion inplace. So, I use the following logic of copying the contents to a dynamically allocated char* array and then doing the conversion: -----------------------------
1
6209
by: Mark | last post by:
I want to replace the following line: sscanf(mybuf,"%s=%s\n",sz1,sz2); with something that produces the same effect, only with dynamic storage i.e string s which is safer. I might try this: sscanf(mybuf.c_str(),"%s=%s\n",string1.c_str(),string2.c_str());
3
8488
by: aquanutz | last post by:
Ok, I have a list of strings (list<string> stringList) that I want to sort alphabetcially, only "sort(stringList.begin(), stringList.end()); ) does not work. Any insight would be helpful. Thanks!
2
2628
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? // test.cpp #include <iostream> #include <fstream> #include <vector> #include <string>
5
2549
by: Martin Jørgensen | last post by:
Hi, The piece of code I'm struggling with is so simple, that I hope nobody wants a complete example for answering the question: -------- string color_line; int data_type = 0; for( vector<string>::const_iterator it = possible_data_types.begin();
2
5882
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the dictionary essentially creates the following array: Key Value
4
3949
by: parez | last post by:
Hi, I am trying to serialize List<List<string>. With the following code public List<List<string>DataRows { get; set; }
0
8305
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
8825
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
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7324
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
6163
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
5632
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
4151
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...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.