954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple Map Question

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

so my syntax is

map 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 >::iterator iter;
iter = test.begin();

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

Thanks for your help.

ff4930
Junior Poster in Training
58 posts since Oct 2007
Reputation Points: 34
Solved Threads: 3
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You