Advertisement

Problems with map<key, value>

Started by May 25, 2000 06:45 PM
1 comment, last by Neophyte 24 years, 6 months ago
I''ve got some problems using C++''s map-function. Basically, I can''t get it to work at all... As far as I''ve gathered from various texts, I should be able to do something like this:

std::map  testMap;

testMap["answer"] = 42;

printf ("The answer is: %d", testMap["answer"]);

 
and get the output "The answer is: 42". All *I* get, however, is compiler-errors. The actual error is something like "class expected" (I don''t remember the exact message). And from mucking about a bit with it it seems that the compile expects 3 arguments within the <>''s. Why? Help appreciated. -Neophyte ~Death awaits you all, with nasty, big, pointy teeth~
The std::map template class needs 3 template parameters.
they are map< key, value, compare >

I''m guessing you wrote std::map&ltstd::string, long >

The standard indicates that for the compare class, it defaults to less&ltkey>

However, your compiler may not support default template parameters.

You must then write

std::map< std::string, long, less&ltstd::string> >

Note that the space between those last two greater than signs is extremely important... you''ll get a boatload of compile errors without it.

Advertisement
Thanks, SteveC, I got it working now.
I had to make my own comparison-function for char*, but it was 1 line of code, so no big deal...

-Neophyte
~Death awaits you all, with nasty, big, pointy teeth~

This topic is closed to new replies.

Advertisement