How to erase & delete pointers to objects stored in a vector?

前端 未结 5 1013
情歌与酒
情歌与酒 2020-12-02 10:17

I have a vector that stores pointers to many objects instantiated dynamically, and I\'m trying to iterate through the vector and remove certain elements (remove from vector

5条回答
  •  眼角桃花
    2020-12-02 10:30

    Once you modify the vector, all outstanding iterators become invalid. In other words, you can't modify the vector while you are iterating through it. Think about what that does to the memory and you'll see why. I suspect that your assert is an "invalid iterator" assert.

    std::vector::erase() returns an iterator that you should use to replace the one you were using. See here.

提交回复
热议问题