开发者

How do I erase part of a vector?

开发者 https://www.devze.com 2023-03-19 08:11 出处:网络
If my std::vector has 1890 elements, and I want to keep the first 1000 and erase 开发者_Python百科the rest,and then again next 890 elements and erase the first 1000,.. so a loop seems to be necessary.

If my std::vector has 1890 elements, and I want to keep the first 1000 and erase 开发者_Python百科the rest,and then again next 890 elements and erase the first 1000,.. so a loop seems to be necessary.

Is there a more convenient way to do this?


std::vector has an erase member function that allows you to erase a range of elements without using an explicit loop. For example:

std::vector<whatever> x(1890);

// erase first 1000 items
x.erase(x.begin(), x.begin()+1000); 
0

精彩评论

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