Erase range from a std::vector?
If my std::vector
has 100 elements, and I only want to keep the first 10 and erase the rest, is there a convenient way 开发者_如何转开发to do this?
Yes, there is an erase function that takes arguments for first and last.
v.erase(v.begin() + 10, v.end());
vec.resize(10); // drops the rest (capacity remains the same)
vec.erase(vec.begin() + 10, vec.begin() + 100);
theVector.erase(theVector.begin() + 10, theVector.begin() + 100);
精彩评论