如果我有一个向量a
的迭代器,然后我从a
移动-构造或移动-赋值向量b
,该迭代器是否仍然指向相同的元素(现在在向量b
中)?下面是我在代码中的意思:
#include <vector>
#include <iostream>
int main(int argc, char *argv[])
{
std::vector<int>::iterator a_iter;
std::vector<int> b;
{
std::vector<int> a{1, 2, 3, 4, 5};
a_iter = a.begin() + 2;
b = std::move(a);
}
std::cout << *a_iter << std::endl; // Is a_iter valid here?
return 0;
}
既然a_iter
已经移动到b
中,那么a
是否仍然有效,或者迭代器是否因移动而失效?参考:std::vector::swap
does not invalidate iterators。
https://stackoverflow.com/questions/11021764
复制相似问题