mercredi 1 juin 2016

Optimizing erase from the end of vector into resize

Is it legal and possible that the compiler optimizes this code:

std::vector<T> my_vec(10);
//some code
my_vec.erase(my_vec.begin()+5,my_vec.end());

into:

std::vector<T> my_vec(10);
//some code
my_vec.resize(5);

I am asking because I always read that std::vector::resize is preferable when erasing from the end over an std::vector::erase. However, I see that std::vector::erase is more readable.

Aucun commentaire:

Enregistrer un commentaire