Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > comparator not called in std::map operations??

Reply
Thread Tools

comparator not called in std::map operations??

 
 
qazmlp
Guest
Posts: n/a
 
      07-04-2004
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: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;
}
--------------------------------------------------------------
 
Reply With Quote
 
 
 
 
Buster
Guest
Posts: n/a
 
      07-04-2004
qazmlp wrote:
> 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.

--
Regards,
Buster.
 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      07-04-2004
On 3 Jul 2004 19:39:03 -0700 in comp.lang.c++,
(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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Optimized comparator ALuPin@web.de VHDL 6 08-25-2005 08:45 AM
counter plus comparator john VHDL 4 11-10-2004 05:41 AM
comparator problem sk VHDL 0 11-03-2004 07:12 PM
Comparator and minimum value address sunil VHDL 4 02-22-2004 08:10 PM
flags vs. comparator Valentin Tihomirov VHDL 5 11-11-2003 05:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57