   nested std::map query
  Hi,I have a nested std::mapit's set up like this:typedef std::map<std::string, std::string> MapValue;typedef std::map<std::string, MapValue> MapHeading;and is for a text based config file system similar to something like unreal tournament, where you have a file with entries such as:[VideoSetup]Width = 1280Height = 1024etcthe 'Heading' such as [VideoSetup] is stored in the string part of MapHeading.Then, the variable such as Width is stored in the first string in MapValue, and the second string contains the value such as 1280What I don't know how to do is actually retrieve values stored in this map setup. I have a function where I pass two strings. One is the heading I want to look under, the second is the variable name I want to get. I know you can use either .find() or the [] accessor, but I don't want to use the latter as it'll create entries in the map if it can't find them, which isn't what I want.I want to use .find(), but the problem with that is if the element doesn't exist, I get an error from the visual studio debugger "map/set iterator not dereferencable"So, What I need to do is first check if the value exists in the map?I can do this to check if the heading value exists:int Config_GetInt(const std::string &heading, const std::string &variable){MapHeading::iterator it;it = myConfig.find(heading);if (it == myConfig.end()){std::cout << "Heading " << heading << " not found!\n";return 0; // should I pass a default value to function and return that if requested value doesn't exist?}which works fine. But, I don't know how to then check a variable exists.it = it->second.find(variable); won't compile. I've tried a few other combinations but I think my understanding of iterators isn't so good.return StringToInt(AvPConfig.find(heading)->second.find(variable)->second);will work, provided the values exists, otherwise it gives me that dereferencable error.Could anyone tell me how to do what I want reliably? Thanks :)   
Quote:Original post by sirlemonheadit = it->second.find(variable); won't compile. I've tried a few other combinations but I think my understanding of iterators isn't so good.No surprise, you defined it as MapHeading::iterator. For the nested map you will need a MapValue::iterator instead. Try this:MapValue::iterator it2 = it->second.find(variable)   ;  Well, you can't use the same iterator with the second map as with the first. Is that the problem you're running into? (It would help if you posted the exact error message, along with the exact code that generates the error.)   ;  The nested map is of a different type, so you need a new iterator variable:int Config_GetInt(const std::string &heading, const std::string &variable){MapHeading::iterator headingIt = myConfig.find(heading);if (headingIt == myConfig.end()){std::cout << "Heading " << heading << " not found!\n";return 0; // should I pass a default value to function and return that if requested value doesn't exist?}   else   {   MapValue::iterator valueIt = headingIt->second.find(variable);   // use valueIt ...   }}Yes, you could use a default value and return it in case something went wrong. You could also return a std::pair<bool, int>, where the bool indicates the item was found and the int is the item if found.Or you could return a boolean indicating if the item is found, and use a reference parameter to write the value back to the calling function.   ;  Ah, you're right. I didn't cop on that the iterator I was trying to reuse was using a different pair type.I get error"Error1error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_Tree<_Traits>::iterator' (or there is no acceptable conversion)d:\development\experiments\experiments\main.cpp97"with the line it = it->second.find(variable);MichaBen's suggestion seems to compile fine. How do I do the equivelant of my "if (it == myConfig.end())" check with this new second iterator?   ;Quote:How do I do the equivelant of my "if (it == myConfig.end())" check with this new second iterator?Same way as with the first. No guarantee I'll get this right, but it should look something like this:header_map_t::const_iterator it1 = header_map.find(header);if (it1 != header_map.end()) {    value_map_t::const_iterator it2 = it1->find(key);    if (it2 != it1->end()) {   return it2->second;    }};  if (it2 != it1->end()) {needs to be if (it2 != it1->second.end()) {Seems to be working fine now :) Thanks everyone!   ;Quote:Original post by sirlemonheadif (it2 != it1->end()) {needs to be if (it2 != it1->second.end()) {Indeed. (Sorry about that - 'not compiled or tested', as they say :)   ;  You're supposed to say you were testing me to make sure I wasn't making you do all the work and thinking for me :p   