从std :: vector中删除前N个元素 [英] Remove first N elements from a std::vector
本文介绍了从std :: vector中删除前N个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我似乎没有想到一个可靠的方法(也压缩内存)从std :: vector中删除前N个元素。
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?
推荐答案
由于你提到要压缩内存,所以最好复制一切到一个新的向量并使用交换惯例。
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<decltype(myvector)::value_type>(myvector.begin()+N, myvector.end()).swap(myvector);
这篇关于从std :: vector中删除前N个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文