| | |
In dealing with a map of maps...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 30
Reputation:
Solved Threads: 2
Hello,
If I have code like such:
How to access the value of the nested map? Normally I access a map's value like such:
To that end, I tried doing this on my nested map:
But this gave me complier errors. What is the correct syntax for this?
If I have code like such:
c++ Syntax (Toggle Plain Text)
map <string, map <string, int> > foo; 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:
c++ Syntax (Toggle Plain Text)
bar->second;
To that end, I tried doing this on my nested map:
c++ Syntax (Toggle Plain Text)
bar->second->second;
But this gave me complier errors. What is the correct syntax for this?
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
Think of
bar->second as a map <string, int> , so simply ... C++ Syntax (Toggle Plain Text)
bar->second["abc"] = 123;
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:
So something like:
C++ Syntax (Toggle Plain Text)
map <string, map <string, int> > foo; // fill map map <string, map <string, int> >::iterator outerit; map <string, int>::iterator innerit; for (outerit = foo.begin(); outerit != foo.end(); ++outerit){ for (innerit = outerit->second.begin(); innerit != outerit->second.end(); ++innerit){ cout << outerit->first << " " << innerit->first << " " << innerit->second << "\n"; } }
Last edited by niek_e; Mar 27th, 2009 at 8:06 am.
•
•
Join Date: Dec 2008
Posts: 30
Reputation:
Solved Threads: 2
•
•
•
•
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:
C++ Syntax (Toggle Plain Text)
map <string, map <string, int> > foo; // fill map map <string, map <string, int> >::iterator outerit; map <string, int>::iterator innerit; for (outerit = foo.begin(); outerit != foo.end(); ++outerit){ for (innerit = outerit->second.begin(); innerit != outerit->second.end(); ++innerit){ cout << outerit->first << " " << innerit->first << " " << innerit->second << "\n"; } }
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:
c++ Syntax (Toggle Plain Text)
outerit = foo.find (some string);
How to move the position of innerit to the value of the key of outerit?
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Need Help on Dealing with Archives. (C++)
Other Threads in the C++ Forum
- Previous Thread: queues
- Next Thread: backgroundWorker2->CancelAsync();
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






