Jump to content
  • Advertisement
Sign in to follow this  
MatrixCubed

STL Question ... Map Key Retrieval?

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

Hello all, I am trying to retrieve keys and data from an STL map. In short, I am reading in arbitrary collections of properties and their values, and storing them in memory. After some processing, I need to be able to retrieve the strings associated with each map entry. For example: Initially I would set something like:
mymap[some_key_read_from_disk] = some_value_read_from_disk;
Then later on in the program I want to be able to:
for (mymap::iterator i = mymap::begin(); i != mymap::end();)
{
    cout << mymap._______ << " is equal to " << mymap._____ << endl;      <---- WHAT GOES HERE?
    i++;
}
Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
I believe there was/is a slight problem in some STL implementations where if you do the following (to check to see if the value exists):

if( map[key] == NULL )

It will create the key as an entry. So be careful with that. Whenever you're checking for something like that, always iterate through it, to avoid creating keys where you don't want them.

I'm not sure what STL implementations have this problem, but I'm sure STLport has fixed it by now. This is probably something from the days of VC++ 6.0. But I still do it that way just to be careful!

Share this post


Link to post
Share on other sites
operator[] always creates a new entry into the map, this is behaviour.. map[key] == something will thus always create a new entry.

this is at least how i got teached..


but it was completely unrelated to the OP's question. his answer, he got yet. ->first and ->second are the two infos he needs.

Share this post


Link to post
Share on other sites
std::map<T>::operator[x]() is syntactic sugar for (*((insert(make_pair(x, T()))).first)).second which means that using operator[] will always add the key to the map if it's not there.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!