Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我的 std::vector 有 1890 个元素,并且我想保留前 1000 个元素并删除其余元素,然后再保留下一个 890 个元素并删除前 1000 个元素,那么循环似乎是必要的。 有没有更方便的方法来做到这一点?
std::vector有一个erase成员函数,允许您在不使用显式循环的情况下擦除一系列元素。例如:
std::vector
erase
std::vector<whatever> x(1890); // erase first 1000 items x.erase(x.begin(), x.begin()+1000);