简体   繁体   中英

Erase range from a std::vector?

如果我的std::vector有100个元素,而我只想保留前10个元素并删除其余元素,那么有一种方便的方法吗?

是的,有一个擦除函数,它接受第一个和最后一个参数。

v.erase(v.begin() + 10, v.end());
vec.resize(10); // drops the rest (capacity remains the same)
theVector.erase(theVector.begin() + 10, theVector.begin() + 100);

vec.erase(vec.begin() + 10, vec.begin() + 100);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM