C 98,向量,删除除第一个元素以外的每个元素

c++ 98, vector, remove every element except the first one

本文关键字:元素 第一个 向量 删除      更新时间:2023-10-16

arr!这就是我这样做的方式,但是有点长。还有更紧凑的吗?(我正在使用CPP 98!(

std::vector<Object> allObjs;
// ... allObjs gets filled ...
// Now get back a vector with only the first element in allObjs
std::vector<Object> justTheFirstElemOfObjs;
justTheFirstElemOfObjs.push_back(allObjs.front());
allObjs = justTheFirstElemOfObjs;

allObjs.resize(1);应该做技巧。