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

map question

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.

map<int, vector<int> > my_map;
map<int, vector<int> >::iterator it;

void erase(int num)
{

for(map::iterator item=my_map.begin();item != my_map.end())
	{
		if(it == num)
			my_map.erase(it++);
		else
		++it;
	}
}
JackDurden
Junior Poster in Training
92 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

looks like there's a couple things, I don't think that would even compile: comparing a map >::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:

map<int, vector<int> > my_map; //I assume these are global or in a class, as you had it
map<int, vector<int> >::iterator it;

void erase(int num)
{
    for(it = my_map.begin(); it != my_map.end(); it++)
    {
        if((*it).first == num)
        {
            my_map.erase(it);
            break;
        }
    }
}


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

~J

jesseb07
Junior Poster
111 posts since Dec 2006
Reputation Points: 76
Solved Threads: 15
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You