Search code examples
c++dictionarynested-map

How to share the key across the different map in c++?


I'm working on map, I stored the data into two different map(it is nested map) having same key, Is their any way to store this data into single DS rather that two different nested maps. Following are my two nested maps:

std::map<KeyStruct, std::map<ClassObjSharedPtr, std::set<ClassObjSharedPtr> > > map1;

std::map<KeyStruct, std::map<ClassObjSharedPtr, std::set<ClassObjSharedPtr> > > map2;

Here map1 and map2 have the same key values.

I have maintained two different maps for specific purpose. Can we store both maps in single DS? So that each key (i.e. each key will have two internal map).


Solution

  • Why not use a single std::map with all the required data?

    Example Code

    struct Data
    {
        std::map<ClassObjSharedPtr, std::set<ClassObjSharedPtr> > mData1;
        std::map<ClassObjSharedPtr, std::set<ClassObjSharedPtr> > mData2;
    };
    
    std::map<KeyStruct, Data> mMapData;