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;
	}
}

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:

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.