Most efficient way of erasing/deleting multiple std::vector elements while retaining original order?

前端 未结 7 1469
隐瞒了意图╮
隐瞒了意图╮ 2020-12-23 17:37


i have a std::vector and a second container holding iterators or indexes (no keys, i want constant access to the element) to this vector for del

7条回答
  •  被撕碎了的回忆
    2020-12-23 18:21

    How about looping through the vector, and for each element that needs to be removed, copy the next element that doesn't need to be removed in to that position. Then when you get to the end, truncate it.

    int last = 0;
    for(int i=0; i= vec.size()) break;
    
       vec[last] = vec[i];   
    }
    
    vec.resize(last);
    

提交回复
热议问题