473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

map<string, map<string, int>>

Can anyone show me how to use a map<string, map<string, int>>?

The declaration of the above data structure works, and I think that I can make some entries, but I'm unable to retrieve the entries.

This is how I'm declaring an iterator: map<string, map<string, int> >::iterator itr;.

Thanks,
Jordon
Jul 13 '07 #1
2 16194
Can anyone show me how to use a map<string, map<string, int>>?

The declaration of the above data structure works, and I think that I can make some entries, but I'm unable to retrieve the entries.

This is how I'm declaring an iterator: map<string, map<string, int> >::iterator itr;.

Thanks,
Jordon
I'm not 100%, but I believe that iterator will only let you step through the outer map, that contains more maps. I assume you want access to the integers in the inner map, I'm not sure what the solution is, but I think that's your problem.

I guess it would be something like this:

map<string, map<string, int>> myMap;
map<string, map<string, int>>::iterator outerIter;
map<string, int>::iterator::innerIter;

outerIter = myMap.begin();
innerIter = outerIter->second.begin();
Jul 13 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
It looks like you have a map of maps.

In your code, itr will point to a map so you use itr-> to call a member function on that map.

To see an element of the second map, you need to find it by key:
Expand|Select|Wrap|Line Numbers
  1. map<string, map<string, int> > MyOuterMap;
  2. map<string, int> MyInnerMap;
  3.  
  4. //Pretend elements arew added
  5. pair<string, map<string, int> > p1(string("Hello"), MyInnerMap);
  6. MyOuterMap.insert(p1);
  7. map<string, map<string, int> >::iterator itr;
  8. string keyouter("Hello");
  9. string keyinner("world");
  10.  
  11. itr = MyOuterMap.find(keyouter);
  12. if (itr == MyOuterMap.end())
  13. {
  14.          //not found
  15.          return -1;
  16. }
  17. map<string, int>::iterator  inneritr;
  18. inneritr = itr->second.find(keyinner);
  19. if (inneritr == itr->second.end())
  20. {
  21.          //not found
  22.          return -1;
  23. }
  24. cout << inneritr->second;
  25.  
Try this.
Jul 13 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: keweiming | last post by:
I have a project which needs to open hundreds to thousands of files for writing. The following is a simplified test program I wrote to see if I can use a map<string, ofstream> object to keep the...
11
by: Martin Jørgensen | last post by:
Hi, - - - - - - - - - - - - - - - #include <iostream> #include <string> #include <map> using namespace std; int main() {
2
Soujiro
by: Soujiro | last post by:
typedef struct { int age; string name; } structure; int functionCall( map< char* , structure* >* map_o ) { map< char* , structure* >* map_op; map_op = map_o;
3
by: asclearuc | last post by:
Hello Is it possible to use map<string&, string&>? Why I need it. I have a large amount of data obtained from XML file. I should do processing of this data. The processing takes many...
13
by: liujiaping | last post by:
Hi, all. I have a dictionary-like file which has the following format: first 4 column 7 is 9 a 23 word 134 .... Every line has two columns....
3
by: eiji.anonremail | last post by:
Hi folks, I have a compile problem on linux, and maybe someone has an idea: #include <map> #include <iostream>
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.