This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Is this a bug or lack of understanding?


Hi

I have the following program:

//----------------------------
#include <map>
#include <string>

struct s {
 bool operator()(string s1, string s2) {
//  return s1.size() < s2.size();
  return s1 < s2;
 }
};

void main()
{
 map<string, int, s> m;
 m["a"] = 1;
 m["ab"] = 1;
 m["ba"] = 1;
 m["abc"] = 1;
 m["acb"] = 1;
 m["bac"] = 1;
 m["bca"] = 1;
 m["cab"] = 1;
 m["cba"] = 1;

 for (map<string, int>::iterator i = b.begin(); i != b.end(); i++)
  cout << (*i).first << "\t" << (*i).second << endl;
}
//-------------------------------------------

If i run it as it stands, it prints out all the elements in the map
in alphabetical order of the keys

If I change the commented return line in the sort function object, to
sort by length of the key, it doesnt print all the entries in the map.

Could someone explain why, or is it a bug?


--
Chris Picton
Usko Communications Systems Developer
Chris.Picton@usko.com



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]