Range-based for loop and std::vector.push_back() crashing the program

后端 未结 2 1906
时光说笑
时光说笑 2020-12-10 09:25
#include 
#include 

int main() {
    std::vector vec;
    for (int i = 0; i < 42; ++i) {
        vec.push_back(i);
              


        
2条回答
  •  隐瞒了意图╮
    2020-12-10 10:09

    Once you call std::vector::push_back or most non-const function for that matter the iterators are invalidated (When the new size exceeds the current capacity of the vector which causes the memory to be re-allocated internally)

    You can find some good references on how standard containers or iterators work in general. I hope that gets you started!

提交回复
热议问题