我正在从 http://www.cplusplus.com/reference/vector/vector/insert/ 学习 std::vector::insert
相关部分代码:
int main ()
{
std::vector<int> myvector (3,100);
std::vector<int>::iterator it;
it = myvector.begin();
it = myvector.insert ( it , 200 );
myvector.insert (it,2,300);
// "it" no longer valid, get a new one:
it = myvector.begin();
为什么.insert
操作后迭代器无效?
最佳答案
来自 this reference它说:
Causes reallocation if the new size() is greater than the old capacity(). If the new size() is greater than capacity(), all iterators and references are invalidated. Otherwise, only the iterators and references before the insertion point remain valid. The past-the-end iterator is also invalidated.
关于C++迭代器有效性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31873475/