Thread: map of maps ??

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    19

    map of maps ??

    in case of single map, to see whether a key exists or not mapname.count(keytofind) is used

    Code:
    map<int,bool>rb;
    map<int,bool>::iterator rb_it;
    
    rb.insert(make_pair(5,true));
    rb.insert(make_pair(7,false));
    rb.insert(make_pair(3,true));
    
    int numtofind=7;
    if(rb.count(numtofind))
    {
              rb_it=rb.find(numtofind);
              cout<<"\n First= "<<(*rb_it).first<< "  Second=  "<<  (*rb_it).second ;
    }
    else
    {
               cout<<"\n The key " << numtofind <<" does not exist in map" ;
    }

    but in case of map of maps, how a key has to be find ??

    Code:
    map<int,map<int,map<int, bool> > >r;
     r[3][4][3]=true;    
     r[3][2][3]=true;
     r[3][3][3]=true;
    how to see whether a particular key exists or not??
    C/C++ IDE: Microsoft visual studio .Net 2003

    Wisdom is the reward for a lifetime of listening... when you'd have preferred to talk.

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    how to see whether a particular key exists or not??
    You call find() for each nested map:
    Code:
    // find r[3][2][3]
    if (r.find(3) != r.end()) {
      if (r[3].find(2) != r[3].end()) {
        if (r[3][2].find(3) != r[3][2].end())
          cout << "Found!\n";
      }
    }
    You might want to wrap that in a function.
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding a Map to a program
    By Shogun32 in forum C++ Programming
    Replies: 1
    Last Post: 05-04-2009, 09:42 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. New editor updates
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-05-2005, 03:26 PM
  5. Creating a map engine.
    By suzakugaiden in forum Game Programming
    Replies: 11
    Last Post: 06-21-2005, 05:06 AM