Keeping the first N elements of a std::vector and removing the rest

Yes, you can use std::vector::resize, which just truncates if the length of the vector is greater than n.

See here: http://www.cplusplus.com/reference/vector/vector/resize/

std::vector<int> myvector;

for (int i=1;i<1000;i++) myvector.push_back(i);

myvector.resize(50);
// myvector will contain values 1..50

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)