Remove first N elements from a std::vector

前端 未结 3 1922
南笙
南笙 2020-12-24 13:42

I can\'t seem to think of a reliable way (that also compacts memory) to remove the first N elements from a std::vector. How would one go about doing that?

3条回答
  •  伪装坚强ぢ
    2020-12-24 13:47

    Since you mention that you want to compact memory, it would be best to copy everything to a new vector and use the swap idiom.

    std::vector(myvector.begin()+N, myvector.end()).swap(myvector);
    

提交回复
热议问题