map question

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

Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

map question

 
0
  #1
Aug 7th, 2009
How do you erase an element thats inside a map. Not the key but the element the key is pointing to? This doesnt seem to be doing what i want it to.

  1. map<int, vector<int> > my_map;
  2. map<int, vector<int> >::iterator it;
  3.  
  4. void erase(int num)
  5. {
  6.  
  7. for(map::iterator item=my_map.begin();item != my_map.end())
  8. {
  9. if(it == num)
  10. my_map.erase(it++);
  11. else
  12. ++it;
  13. }
  14. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 111
Reputation: jesseb07 is on a distinguished road 
Solved Threads: 15
jesseb07's Avatar
jesseb07 jesseb07 is offline Offline
Junior Poster

Re: map question

 
1
  #2
Aug 7th, 2009
looks like there's a couple things, I don't think that would even compile: comparing a map<int, vector<int> >::iterator to an int inside your loop doesn't seem safe to me. As well as not declaring the map type for your iterator.

anyway, see if this is a good starting point:

  1. map<int, vector<int> > my_map; //I assume these are global or in a class, as you had it
  2. map<int, vector<int> >::iterator it;
  3.  
  4. void erase(int num)
  5. {
  6. for(it = my_map.begin(); it != my_map.end(); it++)
  7. {
  8. if((*it).first == num)
  9. {
  10. my_map.erase(it);
  11. break;
  12. }
  13. }
  14. }

I think that's what you were going for, hope that's helpful

~J
Ps. 121

Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html

AJAX, PHP, C#, C++, JAVA
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