Modifying a data structure while iterating over it

前端 未结 6 777
天涯浪人
天涯浪人 2020-12-18 18:50

What happens when you add elements to a data structure such as a vector while iterating over it. Can I not do this?

I tried this and it breaks:

int m         


        
6条回答
  •  星月不相逢
    2020-12-18 19:07

    if you need to do it this way, you can reserve the maximum number of records you could add. this will stop the vector from needing to resize, and this should prevent crashes

提交回复
热议问题