The βnormalβ way to remove an element from a vector is as follows:
vec.erase(vec.begin() + index);
But theoretically this is faster, just for this:
if (vec.size() >= 2) { std::swap(vec.begin() + index, vec.end() - 1); vec.pop_back(); } else { vec.clear(); }
Is there a reason not to use the latter?
source share