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 JHrgensen
--
---------------------------------------------------------------------------
Home of Martin JHrgensen - http://www.martinjoergensen.dk