Jump to content
  • Advertisement
Sign in to follow this  
SIGURD42

Simple STL Map Question [Solved]

This topic is 4428 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry for this really simple question but Ive tried for ages to figure this out and Ive created a simple demo to illustrate my confusion:
map<const char*, int> my_map;
string str = "test";
int test;

my_map[str.data()] = 5;

test = my_map["test"];
cout << test << '\n';
why doesnt this work? My output is '0'. If I change my_map[str.data()] = 5; to my_map["test"] = 5; or test = my_map["test"]; to test = my_map[str.data()]; it works fine but why? simply put: whats the difference between my str.data() and the chars "test"?? [Edited by - SIGURD42 on May 7, 2006 11:12:03 AM]

Share this post


Link to post
Share on other sites
Advertisement
Have you worked with pointers? char* is just a pointer pointing to the start of chars, therefore if "test" and str.data() points to different areas in memory they are different, no matter what they points to. This is one of the issues people talk about when they say you shouldn't use char* in C++, you should instead use std::string (or std::wstring for a wchar_t string).

Share this post


Link to post
Share on other sites
My guess is that your map is try to compare pointers(const char*) to a std::string(my_map["test"]) which won't work(so you get 0 - a new int). This of course won't work. Even if you passed the indexer a const char* it would have a different location and still wouldn't work. I suggest you just make the map key a std::string since it is an object with an actual comparator.

Edit: damn I'm way too slow.

Share this post


Link to post
Share on other sites
By default, std::map uses the == operator to compare keys.

If you specify a char* as the key, then the map will compare the pointers, not the text of the string. There is absolutely no guarantee that two identical string literals, say, "test" and "test" will have the same address. That's why, in C, you use strcmp to compare them.

str.data returns a pointer to the data in the string object. Since you used that pointer as the key for the map, and are re-using the pointer later (the pointer remains valid until you modify the string), the map manages to find it.

The fix is simple: use map<string, int> instead. Then it will use string's overloaded operator ==.

Share this post


Link to post
Share on other sites
Thank you! Ive only just started using the STL you see and am learning it from what seems to be a really crappy source. All I needed was you guys! Thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!