469,951 Members | 2,763 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

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 7925
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()(const 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_string'
which in turn calls the 'compare' member of 'std::basic_string'. 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()(const 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_string'
which in turn calls the 'compare' member of 'std::basic_string'. 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()(const _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<class T>
struct less : public binary_function<T, T, bool> {
bool operator()(const 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
Larry I Smith wrote:
Martin Jørgensen wrote: -snip-
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()(const _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...


Damn... I wanted to ask you where it was :-)

But that was not so hard... Only 1 option:

find . | xargs grep -l "operator <"
../bits/basic_string.h
Apple:/usr/include/c++/4.0.0 mac$ cd bits
Apple:/usr/include/c++/4.0.0/bits mac$ vi basic_string.h

Gives:

// operator <
/**
* @brief Test if string precedes string.
* @param lhs First string.
* @param rhs Second string.
* @return True if @a lhs precedes @a rhs. False otherwise.
*/
template<typename _CharT, typename _Traits, typename _Alloc>
inline bool
operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
const basic_string<_CharT, _Traits, _Alloc>& __rhs)
{ return __lhs.compare(__rhs) < 0; }

I guess then you ask me where is __lhs.compare?

Is it this?

template<typename _CharT, typename _Traits, typename _Alloc>
int
basic_string<_CharT, _Traits, _Alloc>::
compare(size_type __pos, size_type __n, const basic_string& __str)
const
{
_M_check(__pos, "basic_string::compare");
__n = _M_limit(__pos, __n);
const size_type __osize = __str.size();
const size_type __len = std::min(__n, __osize);
int __r = traits_type::compare(_M_data() + __pos, __str.data(),
__len);
if (!__r)
__r = __n - __osize;
return __r;
}

Found in basic_string.tcc... I don't understand this _CharT + _Traits +
Alloc... I guess it's inside the string-class... Could it be that is
defined in basic_string.tcc ??? That's a really crazy file... Lot's of
dynamic memory allocation, but that's also what I would expect of a
C++-string class... It's actually a good exercise to try and find this
stuff but I get so completely confused by jumping in and out of
different files that uses a lot of really weird variable names.

Why did the guys who made this use variable names with so many
underscoes __? That looks confusing, IMO...
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 14 '06 #11
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...


That's not what I said. I said to look for a file with "functional" in
its *name*. I did it myself and found about four files, all of which
were relevant to varying degrees.
May 14 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Matt Garman | last post: by
37 posts views Thread by Zombie | last post: by
3 posts views Thread by aquanutz | last post: by
5 posts views Thread by Martin Jørgensen | last post: by
4 posts views Thread by parez | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.