Archived

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

std::map question

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

I have a map, in which I store const char* values and use const char* for the keys aswell. So when I have my map stored with keys and all, which are dynamic, I want to store the data on disk at some point. However, I need the key and the value for that inside the file. To access each value of the map I use iterators, code below hasn''t been tested yet
map::iterator Begin;
Begin = Variables.begin();

// No idea if this is correct, haven''t tried it
szKey = Variables[Begin + Value].key;
szValue = Variables[Begin + Value].data;
 
How can I access the key value? Sand Hawk

Share this post


Link to post
Share on other sites
Something like this?
  
map<const char*, const char*, ltstr>::iterator i, end = Variables.end();

for (i = Variables.begin(); i != end; ++i)
{
const char* key = i->first;
const char* value = i->second;
// do stuff

}
If you need it, there''s also a function for incrementing an iterator a given number of times: advance(iter, dist).

Share this post


Link to post
Share on other sites
BeerHunter posted the answer ... i''m just going to add a little info

1. When you access items in a map using the square bracket operators aka


  
value = myMap[key];
//OR

myMap[key] = newValue;


, what you get back is an lvalue for a data item ... and there is no way to access or modify the key itself with such expressions.


2. When you iterate through a map, the iterator is a pointer to a std:air, a pair has two elements called first and second. In the case of a map, the first element is the key, and the second element is the associated data / value for that key.

3. if you want to post source code on this forum, try using source tags ... you put [ source ] and [ /source ] around the code you want to post (but without the extra spaces inside the brackets.

Feel free to make another post in this topic just to test it out ...

Share this post


Link to post
Share on other sites
a) A map''s key type is automatically const
b) A map''s data type cannot be const
c) If you use char* without a custom comparison op, the map sorts the pointers, not the strings
d) using a map<string, string> would be much easier


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
I already knew how to access the data by refering the key e.g. Map["Blah"].

Also, I have a custom comparison for the strings to sort, altrhough sorting is not necessary in this case, just quick access of items by key.

Thx for showing me the ->first and the ->second.

Sand Hawk

Share this post


Link to post
Share on other sites