jeudi 29 août 2019

why and when do we reserve memory for vectors to add new elements to a std::vector

For std::vector i can add new elements using Push_Back.

std::vector< int > Channels;
Channels.push_Back(5);
Channels.push_Back(7);
Channels.push_Back(8);

why should i reserve memory first and than add elements.

 Channels.reserve( 3 );
 Channels.push_Back(5);
 Channels.push_Back(7);
 Channels.push_Back(8);

Aucun commentaire:

Enregistrer un commentaire