c ++ 98,向量,删除除第一个元素之外的所有元素

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

啊!我就是这样做的,但是有点长。还有更紧凑的吗? (我使用的是 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); 应该可以解决问题。