nested std::map using pointers

I am using a map inside a map and want to access a specific member in the second map.

std::map<int, std::map<DWORD,IDLL::CClass*>*> MyMap



auto outerIt = MyMap.find(someInt); if(outerIt != MyMap.end()) { auto innerIt = (*outerIt)->find(someDWord); if(innerIt != (*outerIt)->end()) { auto yourElement = *innerIt; } }


If you are sure the keys exist, you could also try:

IDLL::CClass* x = (*MyMap[key1])[key2];


You can use std::map::find in two steps: first to find the value associated with the key in the outer map, and then repeat for the inner map.

The following compilable code seems to work with VS2010 SP1 (VC10):

#include <iostream> #include <map> typedef unsigned int DWORD; namespace IDLL { struct CClass { CClass() : n(0) {} explicit CClass(int nn) : n(nn) {} int n; }; } // namespace IDLL int main() { // // Prepare maps for testing // std::map<int, std::map<DWORD,IDLL::CClass*>*> MyMap; IDLL::CClass c1(10); IDLL::CClass c2(20); std::map<DWORD, IDLL::CClass*> m1; m1[10] = &c1; m1[20] = &c2; MyMap[30] = &m1; // // Testing code for maps access // const int keyOuter = 30; auto itOuter = MyMap.find(keyOuter); if (itOuter != MyMap.end()) { // Key present in outer map. // Repeat find in inner map. auto innerMapPtr = itOuter->second; const DWORD keyInner = 20; auto itInner = innerMapPtr->find(keyInner); if (itInner != innerMapPtr->end()) { IDLL::CClass * classPtr = itInner->second; std::cout << classPtr->n << '\n'; } } }


If you aren't sure the keys exist:

std::map<DWORD,IDLL::CClass*>* inner = MyMap[key1]; IDLL::CClass* x = 0; if(inner) x = (*inner)[key2]; if(x) std::cout << "Winner! " << *x << "\n"; else std::cout << "Loser.\n";


  • How to call two functions and use their results as arguments for each other?
  • Passing arguments by pass-by-reference to a swap function
  • Using ANT to generate a .jar in a very large project
  • How to copy hashset and hashmap, and does the Java use pointers?
  • jQuery center element to viewport using center plugin
  • Exception Handling in Ada during Unit Test
  • VBA Refresh UserForm ListBox Data when source changes
  • Angular http request with json param
  • Iterating and modifying JSON in Qt
  • How to specify a java.util.HashMap (key ==> value) in wsdl
  • How to Remove Duplication from Spring 3 MVC Standard and Ajax Request Controllers and Views
  • Truncate dictionary list values
  • get key value pairs from xml using linq
  • Foreign key pointing to Compound Key
  • Why the query doesn't return results when the ancestor is not provided?
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • Array with custom indexes in Ionic2
  • Jackson Parser: ignore deserializing for type mismatch
  • Xcode 4 NSLog Macro link in Xcode 3
  • Change multiple background-images with jQuery
  • Check if a string to interpolate provides expected placeholders
  • Trying to switch camera back to front but getting exception
  • Javascript + PHP Encryption with pidCrypt
  • Display Images one by one with next and previous functionality
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Matplotlib draw Spline from multiple points
  • XCode can't find symbols for a specific iOS library/framework project
  • Calling of Constructors in a Java
  • Compare two NSDates in iPhone
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Transpose CSV data with awk (pivot transformation)
  • Use group_by to filter specific cases while keeping NAs
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • File not found error Google Drive API
  • Qt: Run a script BEFORE make
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal