Could you help me with an efficient way to shrink an std::vector<T>
from N1
down to N2
(N2 <= N1
) items in it without requiring T
to have a default constructor (i.e. resize()
is not an option because it requires a default constructor because it can be used to grow also), and using only move semantics (without requiring a copy constructor/assignment operator in T
)? Whether the operation actually shrinks the allocated memory is optional (I haven't yet decided which one is better for my program).
What I have tried so far:
template<typename T> void Filter(vector<T> &v) {
// ... Filter the N1 items of the vector and move them to the first N2 positions
vector<T>(move(v)).swap(v); // this is wrong
}
Aucun commentaire:
Enregistrer un commentaire