开发者

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜