Removing item from vector while iterating?

后端 未结 8 2189
小鲜肉
小鲜肉 2020-11-27 15:40

I have a vector that holds items that are either active or inactive. I want the size of this vector to stay small for performance issues, so I want items that have been mark

8条回答
  •  孤街浪徒
    2020-11-27 16:41

    erase returns a pointer to the next iterator value (same as Vassilis):

    vector ::iterator mit
    for(mit = myVec.begin(); mit != myVec.end(); )
    {   if(condition)
            mit = myVec.erase(mit);
        else
            mit++;
    }
    

提交回复
热议问题