问题描述
迭代的正确方法是使用迭代器.但是,我认为通过擦除,迭代器无效.
The proper way to iterate is to use iterators. However, I think by erasing, the iterator is invalidated.
基本上我想做的是:
如果没有 v[i] 方法,我怎么能做到这一点?
How could I do this without v[i] method?
谢谢
推荐答案
erase()
返回一个新的迭代器:
erase()
returns a new iterator:
请注意,我们不能再将它与预先计算的结束进行比较,因为我们可能会删除它并因此使其无效.我们每次都必须明确地结束.
Note that we can no longer compare it against a precalculated end, because we may erase it and therefore invalidate it. We must get the end explicitly each time.
更好的方法可能是结合 std::remove_if
和 erase()
.你从 O(N2)(每个元素都会被擦除并随着你的移动而移动)变成 O(N):
A better method might be to combine std::remove_if
and erase()
. You change from being O(N2) (every element gets erased and shifted as you go) to O(N):
其中 pred
是您的移除谓词,例如:
Where pred
is your removal predicate, such as:
在你的情况下,你可以让它变得非常通用:
In your case, you can make it pretty general:
注意 lambda 的存在是为了简化这个过程,在 Boost 和 C++11 中都是如此.
Note lambda's exist to simplify this process, both in Boost and C++11.
这篇关于在为每个执行 a 时从 std::vector 中擦除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!