Start New Discussion within our Software Development Community

Hello everyone,

I am new to maps and I have a simple question.

I have a map that stores a string as key and the data I want a map<int, int>

so my syntax is

map<string, map<int, int> test;

What would be the syntax to insert say "hello" and say 3,3?

test.insert( make_pair("hello", ???

Im stuck here.

Also, how would I display the contents?
map<string, map<int, int> >::iterator iter;
iter = test.begin();

cout << iter->first << endl;
cout << (int)iter->second->first;?????

Thanks for your help.

>What would be the syntax to insert say "hello" and say 3,3?

test.insert ( make_pair ( "hello",  map<int, int>() ) );
test["hello"].insert ( make_pair ( 3, 3 ) );

Don't forget that you need to create a new map for the second half of the pair.

>Also, how would I display the contents?

map<string, map<int, int> >::iterator iter = test.begin();

cout<< iter->first <<'\n';
cout<< iter->second[3] <<'\n';

Once again, remember that the second half of the pair is a completely separate map. iter->second gives you the map object, not an iterator.

This question has already been answered. Start a new discussion instead.