In dealing with a map of maps...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 30
Reputation: ItecKid is an unknown quantity at this point 
Solved Threads: 2
ItecKid ItecKid is offline Offline
Light Poster

In dealing with a map of maps...

 
0
  #1
Mar 27th, 2009
Hello,

If I have code like such:

  1. map <string, map <string, int> > foo;
  2. map <string, map <string, int> >::iterator bar;

How to access the value of the nested map? Normally I access a map's value like such:

  1. bar->second;

To that end, I tried doing this on my nested map:
  1. bar->second->second;

But this gave me complier errors. What is the correct syntax for this?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: In dealing with a map of maps...

 
0
  #2
Mar 27th, 2009
Think of bar->second as a map <string, int> , so simply ...
  1. bar->second["abc"] = 123;
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,841
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: In dealing with a map of maps...

 
0
  #3
Mar 27th, 2009
If you want to read through all your maps in your map (...), you need two different iterators. One for a map <string, int> and one for a map<string, map<string, int> >.
So something like:
  1. map <string, map <string, int> > foo;
  2. // fill map
  3. map <string, map <string, int> >::iterator outerit;
  4. map <string, int>::iterator innerit;
  5. for (outerit = foo.begin(); outerit != foo.end(); ++outerit){
  6. for (innerit = outerit->second.begin(); innerit != outerit->second.end(); ++innerit){
  7. cout << outerit->first << " " << innerit->first << " " << innerit->second << "\n";
  8. }
  9. }
Last edited by niek_e; Mar 27th, 2009 at 8:06 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 30
Reputation: ItecKid is an unknown quantity at this point 
Solved Threads: 2
ItecKid ItecKid is offline Offline
Light Poster

Re: In dealing with a map of maps...

 
0
  #4
Mar 27th, 2009
Originally Posted by niek_e View Post
If you want to read through all your maps in your map (...), you need two different iterators. One for a map <string, int> and one for a map<string, map<string, int> >.
So something like:
  1. map <string, map <string, int> > foo;
  2. // fill map
  3. map <string, map <string, int> >::iterator outerit;
  4. map <string, int>::iterator innerit;
  5. for (outerit = foo.begin(); outerit != foo.end(); ++outerit){
  6. for (innerit = outerit->second.begin(); innerit != outerit->second.end(); ++innerit){
  7. cout << outerit->first << " " << innerit->first << " " << innerit->second << "\n";
  8. }
  9. }
Hello,

Thank you very much for your reply. I have one more question. Due to assignment parameters, the performance of this function must be sub-linear. (i.e, no for loops). So if I have code above, and do this:

  1. outerit = foo.find (some string);

How to move the position of innerit to the value of the key of outerit?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC