How to delete an element from a vector while looping over it?

前端 未结 6 2060
时光取名叫无心
时光取名叫无心 2020-11-27 17:41

I am looping through a vector with a loop such as for(int i = 0; i < vec.size(); i++). Within this loop, I check a condition on the element at that vector i

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 18:23

    Iterate over the vector backwards. That way, you don't nuke the ability to get to the elements you haven't visited yet.

提交回复
热议问题