Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++98

Arr! This is how I do it, but it's kinda long. Anything more compact? (I am using 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;

1 Answers

allObjs.resize(1); should do the trick.

like image 187
Rosme Avatar answered Dec 12 '25 02:12

Rosme



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!