Remove first N elements from a std::vector

前端 未结 3 1928
南笙
南笙 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:53

    v.erase( v.begin(), v.size() > N ?  v.begin() + N : v.end() );
    

    Don't forget the check of the size, just in case.

提交回复
热议问题