Q:

Further std::set woes

I asked this question earlier. I am intrigued by std::set but I have another confusing scenario.
Namely, is the following code legal, portable c++ for T=std::vector and T=std::set:
template <typename T>
void remove_elements(T& collection, int removal_value)
{
    typename T::iterator new_end = 
        std::remove(collection.begin(), collection.end(), removal_value);
    collection.erase(new_end, collection.end());
}

According to the SGI reference, set::iterator and set::const_iterator are the same, so I don't know if this is legit. However, I can't seem to find another way to get the operation I need to work regardless of type. I could resort to template specialization, however it would be nice not to have to specialize in the first place.

A:

erase-remove idiom works only for sequence containers. For the standard associative containers it will not work and the solution is not so straightforward. 
Two approaches :

Use remove_copy_if to copy all the
values into another temporary
container and then swap the contents
of the original container with those
of temporary one. [Refer my answer to related Q]
The other one would be loop to walk
the container elements and post increment the iterator when you pass it to erase.

Refer this Q for more details: remove_if equivalent for std::map 
Also, refer Item 9. Choose carefully among erasing options from Effective STL by Scott Meyers.

