[Cs3] getting rid of extra capacity in a vector

Mikhail Nesterenko mikhail at cs.kent.edu
Tue Feb 9 14:23:00 EST 2021


CS3 students,

This is embarrassing, but the name that escaped me is Scott
Meyers. He, btw, advocates using deques over vectors for their
consistency of space allocation. Here is his trick of getting rid of
excess capacity in a vector. Next time during the lecture, explain to
me why it works.

   vector <int> bigV;
   // here goes the code that leaves bigV with excess capacity

   // this code trims excess capacity of bigV
   {
	vector <int> tmpV;
  	bigV.swap(tmpV);
   }

Thanks,
-- 
Mikhail


More information about the cs3 mailing list