Reserving Space in a Vector : vector reserve « vector « C++ Tutorial






#include <vector>

using namespace std;

int main( )
{
   // fill a vector with one million numbers the slow way
   vector<int> v1;
   for( int i = 0; i < 1000000; ++i )
      v1.push_back( i );

   // fill a vector with one million numbers the fast way
   vector<int> v2;        // has zero elements
   v2.reserve( 1000000 ); // still has zero elements
   for( int i = 0; i < 1000000; ++i )
      v2.push_back( i );
}








16.21.vector reserve
16.21.1.Reserving Space in a Vector
16.21.2.Reserve memory for five elements to avoid reallocation
16.21.3.Ensure there's room for at least 1000 elements