Removing item from vector while iterating?

后端 未结 8 2173
小鲜肉
小鲜肉 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:34

    I agree with wilx's answer. Here is an implementation:

    // curFiles is: vector < string > curFiles;
    
    vector< string >::iterator it = curFiles.begin();
    
    while(it != curFiles.end()) {
    
        if(aConditionIsMet) {
    
            it = curFiles.erase(it);
        }
        else ++it;
    }
    

提交回复
热议问题