Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Nested STL maps?

Nested STL maps?

Scheduled Pinned Locked Moved ATL / WTL / STL
helpcsharpc++debuggingperformance
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dbunder
    wrote on last edited by
    #1

    Currently writing a memory management system and I decided it'd be the logical thing to use stl maps to store various data. I'm very new to stl so I don't really understand the ins and outs of it yet, so I'm stuck on a problem which *should* be easy, but I'm doing something wrong. I'm basically keeping a map of maps, with a unique string as the key to each of the maps. Here's one of my insert functions: bool CHeapStore::_insert (heapHeader_t *header) { map tmpMap; pair< map >::iterator, bool> p; tmpMap.insert(pair(header->id, header)); p = s_heaps.insert(pair >(header->name, tmpMap)); s_mmStashing = false; return p.second == true ? true : false; } The program crashes on the s_heaps.insert() and returns the debugger to this in xtree: _Pairib insert(const value_type& _Val) { // try to insert node with value _Val _Nodeptr _Trynode = _Root(); // crashes here What am I doing wrong? I've also tried other insertion methods, but this is the most direct one. The s_heap is an empty map when it crashes, it's the first call to the insert function. Thank you so much for any help. btw, i'm using the default stl library that comes with MSVC .NET 2003. -j

    L P 2 Replies Last reply
    0
    • D dbunder

      Currently writing a memory management system and I decided it'd be the logical thing to use stl maps to store various data. I'm very new to stl so I don't really understand the ins and outs of it yet, so I'm stuck on a problem which *should* be easy, but I'm doing something wrong. I'm basically keeping a map of maps, with a unique string as the key to each of the maps. Here's one of my insert functions: bool CHeapStore::_insert (heapHeader_t *header) { map tmpMap; pair< map >::iterator, bool> p; tmpMap.insert(pair(header->id, header)); p = s_heaps.insert(pair >(header->name, tmpMap)); s_mmStashing = false; return p.second == true ? true : false; } The program crashes on the s_heaps.insert() and returns the debugger to this in xtree: _Pairib insert(const value_type& _Val) { // try to insert node with value _Val _Nodeptr _Trynode = _Root(); // crashes here What am I doing wrong? I've also tried other insertion methods, but this is the most direct one. The s_heap is an empty map when it crashes, it's the first call to the insert function. Thank you so much for any help. btw, i'm using the default stl library that comes with MSVC .NET 2003. -j

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If you are using a map of maps and don't care about existence before inserting (you are willing to overwrite values if the keys match) then try the following: declaration: map < string , map < int , heapHeader_t* > > storage. To insert, just use the subscripts for access. If the items does not exist in the map, the map will automatically insert the item using the keys from the subscripts. storage[string][int] = headHeader_t* item; But removing items from the map is different question. Hopefully this is helpful. Glenn

      1 Reply Last reply
      0
      • D dbunder

        Currently writing a memory management system and I decided it'd be the logical thing to use stl maps to store various data. I'm very new to stl so I don't really understand the ins and outs of it yet, so I'm stuck on a problem which *should* be easy, but I'm doing something wrong. I'm basically keeping a map of maps, with a unique string as the key to each of the maps. Here's one of my insert functions: bool CHeapStore::_insert (heapHeader_t *header) { map tmpMap; pair< map >::iterator, bool> p; tmpMap.insert(pair(header->id, header)); p = s_heaps.insert(pair >(header->name, tmpMap)); s_mmStashing = false; return p.second == true ? true : false; } The program crashes on the s_heaps.insert() and returns the debugger to this in xtree: _Pairib insert(const value_type& _Val) { // try to insert node with value _Val _Nodeptr _Trynode = _Root(); // crashes here What am I doing wrong? I've also tried other insertion methods, but this is the most direct one. The s_heap is an empty map when it crashes, it's the first call to the insert function. Thank you so much for any help. btw, i'm using the default stl library that comes with MSVC .NET 2003. -j

        P Offline
        P Offline
        Paul Ranson
        wrote on last edited by
        #3

        I converted your code into a small test program,

        #include <map>
        #include <string>

        struct heapHeader
        {
        std::string name_ ;
        int id_ ;
        void * p_ ;
        } ;

        struct heap
        {
        std::map<std::string, std::map<int, heapHeader *> > s_heaps ;

        bool insert ( heapHeader \*header)
        {
            std::map<int, heapHeader \*> tmpMap;
            std::pair< std::map<std::string, std::map<int, heapHeader \*> >::iterator, bool> p;
        
            tmpMap.insert(std::pair<int, heapHeader \*>(header->id\_, header));
            p = s\_heaps.insert(std::pair<std::string, std::map<int, heapHeader \*> >(header->name\_, tmpMap));
            
            return p.second ;
        }
        

        } ;

        int main( )
        {
        heap h ;
        heapHeader * ph = new heapHeader ;
        ph->id_ = 0 ;

        h.insert ( ph ) ;
        
        return 0;
        

        }

        and it compiles and runs as expected. So I don't know what your problem is. However I think your code is wrong. In the first place I wouldn't use a map where the key is embedded in the type, a set would be more efficient, you can create specific predicates to allow the object to be sorted according to its id or name. Secondly what if you insert a subsequent heapHeader with the same name? I assume this is legal? This way your maps of id-heapheader would have more than one member. Anyway I would consider rewriting 'insert' along these lines,

        void insert ( heapHeader \*header)
        

        {
        std::pair< std::mapname_, std::map<int, heapHeader *> ()));
        (*p.first).second.insert ( std::make_pair ( header->id_, header )) ;
        }

        Although perhaps the syntax could be tidied. The point is that std::map::insert never fails (other than through memory exhaution, in which case it throws an exception), it either inserts a new entry and returns 'true' in the second part of the pair, or returns false. In either case the first part of the return is an iterator to the appropriate entry. Hope that made some sense. Paul

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups