Simple Map Question

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2007
Posts: 58
Reputation: ff4930 is an unknown quantity at this point 
Solved Threads: 2
ff4930 ff4930 is offline Offline
Junior Poster in Training

Simple Map Question

 
0
  #1
Jun 24th, 2008
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.
Last edited by ff4930; Jun 24th, 2008 at 11:49 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,603
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Simple Map Question

 
0
  #2
Jun 24th, 2008
>What would be the syntax to insert say "hello" and say 3,3?
  1. test.insert ( make_pair ( "hello", map<int, int>() ) );
  2. 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?
  1. map<string, map<int, int> >::iterator iter = test.begin();
  2.  
  3. cout<< iter->first <<'\n';
  4. 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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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