开发者

Erase range from a std::vector?

开发者 https://www.devze.com 2023-02-17 12:53 出处:网络
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

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

精彩评论

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