Which C++ Standard Library wrapper functions do you use?

Quite often I’d use vector as a set of items in no particular order (and, obviously, when I don’t need fast is-this-element-in-the-set checks). In these cases, calling erase() is a waste of time since it will reorder the elements and I don’t care about order. That’s when the O(1) function below comes in handy – just move the last element at the position of the one you’d want to delete:

template<typename T>
void erase_unordered(std::vector<T>& v, size_t index)
{
    v[index] = v.back();
    v.pop_back();
}

Leave a Comment