473,508 Members | 2,428 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 8738
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
5041
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...
1
17908
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...
37
7307
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...
1
6201
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:...
3
8480
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
2616
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 ? //...
5
2533
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(...
2
5857
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...
4
3932
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
7231
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,...
0
7133
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...
0
7336
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,...
0
7504
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...
0
5643
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,...
1
5059
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...
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.