【发布时间】:2011-07-17 01:20:35
【问题描述】:
如果我的std::vector 有 100 个元素,而我只想保留前 10 个并删除其余的,有没有方便的方法来做到这一点?
【问题讨论】:
标签: c++
如果我的std::vector 有 100 个元素,而我只想保留前 10 个并删除其余的,有没有方便的方法来做到这一点?
【问题讨论】:
标签: c++
是的,有一个 erase 函数接受 first 和 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);
【讨论】: