Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Wildcorn

std::map - how can I map by content or a string rather than memory location?

This topic is 5593 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

In my project I''m trying to separate the date from logic, so I''m trying to make a system that imports item lists from text files. The trouble is I want to some how map each ones 8 character IDcode to the actual object in memory. So in the standard library I''m using this:
using namespace std;
map  items;
 
however instead of doing what I want, this just stores them based on the char*, not the actual string represented by the char*. Even though the contents of the pointer have changed, the actual pointer has not so all the data over rights itself. Is there a way to get this to work the way I want? Would it work if it where a string rather than a char array?

Share this post


Link to post
Share on other sites
Advertisement
That fixed it nicely =). Does the standard library make special case for strings? I don''t really understand why a string works and a character array wouldn''t

Share this post


Link to post
Share on other sites
The name of any array is a pointer to the first element of the array. char * is not a string, it is a pointer. The same for "char array[x]", array is a constant pointer, so when you map something by char * you are telling it to compare the addresses, not the string data. Since the std::string class overloads operators like < and >, it maps the actual string data instead of the address of the string.

Share this post


Link to post
Share on other sites

  • 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!