c++ - 从 C++ vector 中推送和检索值的意外输出

标签 c++ c++11 vector iterator

我编写了以下程序用于将值推送和打印到 vector 中。似乎如果我从 vector 中推送和打印值,它会给我未定义的行为。例如,我的预期输出应该是:(1, 3, 4, 5) 而我得到的输出是 (0, 3, 4, 5)。有人可以解释我哪里出错了。

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<unsigned> vec;
    vec.push_back(1); vec.push_back(3); vec.push_back(4); vec.push_back(5);

    for(vector<unsigned>::iterator i=vec.begin(), l=vec.end(); i!=l; ++i){
        vec.push_back(2);
        cout<<(*i)<<"\n";
    }

    return 0;
}

最佳答案

是的,这是未定义的行为。当std::vector::push_back在循环内部被调用,迭代器i 可能会失效(如果重新分配发生),然后像*i 一样对其进行操作是UB。而l肯定会失效。

If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated. Otherwise only the past-the-end iterator is invalidated.

你可以使用 std::vector::reserve以避免重新分配,从而避免 i 失效。例如

vector<unsigned> vec;
vec.push_back(1); vec.push_back(3); vec.push_back(4); vec.push_back(5);

vec.reserve(vec.size() * 2);
for(vector<unsigned>::iterator i = vec.begin(); i != vec.end(); ++i) {
    cout<<(*i)<<"\n";
    vec.push_back(2);
    ++i;
    cout<<(*i)<<"\n";
}

请注意,您应该直接使用 vec.end() 以避免 l(尾后迭代器)和 ++i 失效 在迭代结束时两次以避免无限循环。

关于c++ - 从 C++ vector 中推送和检索值的意外输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42604865/

相关文章:

c++ - 将 std::move 与 std::shared_ptr 一起使用

r - 摆脱附加字符向量的循环

c++ - 按计数对 multimap 进行排序

c++ - AES - 如何在 C++ 中编写逆子字节?

c++ - 如何声明模板类成员类型的指针/引用?

c++ - 优化 C++11 随机生成器的使用

haskell - 在Haskell中,如何将整数转换为代理(n::KnownNat),其中n等于整数?

c++ - 需要一个从 vector 派生的 vector

c++ - OpenCV 中的海报检测?

c++ - 使用类模板 :error LNK2019 的 C++ 程序