comparator not called in std::map operations??

Q

qazmlp

The following code is taken from:
http://www.sgi.com/tech/stl/Map.html

As the keytype is 'const char*' in the std::map object, there is a
special comparator function used here. But, when I run this program,
I never get "Inside comparator" printed. What exactly is happening?

I am just wondering how the insertion( via std::map::eek:perator[]()) and
std::map::find() works properly without the comparator function
getting used.

Kindly clarify!
--------------------------------------
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
std::cout << "Inside comparator\n" ;
return strcmp(s1, s2) < 0;
}
};

int main()
{
map<const char*, int, ltstr> months;

months["january"] = 31;
months["february"] = 28;
months["march"] = 31;
months["april"] = 30;
months["may"] = 31;
months["june"] = 30;
months["july"] = 31;
months["august"] = 31;
months["september"] = 30;
months["october"] = 31;
months["november"] = 30;
months["december"] = 31;

cout << "june -> " << months["june"] << endl;
map<const char*, int, ltstr>::iterator cur = months.find("june");
map<const char*, int, ltstr>::iterator prev = cur;
map<const char*, int, ltstr>::iterator next = cur;
++next;
--prev;
cout << "Previous (in alphabetical order) is " << (*prev).first <<
endl;
cout << "Next (in alphabetical order) is " << (*next).first << endl;
}
--------------------------------------------------------------
 
B

Buster

qazmlp said:
The following code is taken from:
http://www.sgi.com/tech/stl/Map.html

As the keytype is 'const char*' in the std::map object, there is a
special comparator function used here. But, when I run this program,
I never get "Inside comparator" printed. What exactly is happening?

Your compiler and/or standard library implementation is broken.
 
D

David Harmon

On 3 Jul 2004 19:39:03 -0700 in comp.lang.c++, (e-mail address removed)
(qazmlp) wrote,
special comparator function used here. But, when I run this program,
I never get "Inside comparator" printed. What exactly is happening?

Works for me including "Inside comparator" messages with Digital Mars
C++ compiler and STLPORT, free download from http://www.digitalmars.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top