Mega Code Archive

 
Categories / C++ Tutorial / Vector
 

Make a big vector and then minimize its memory

#include <algorithm> #include <iostream> #include <vector> using namespace std; int main( ) {    const int big_size = 10000;    vector<double> v( big_size );    // make a big vector and then minimize its memory    v.assign( big_size, 3.33 );    cout << "\n\nBefore resizing, the capacity of the vector is "       << v.capacity() << " and its size is " << v.size();    v.resize( 1 );    cout << "\nAfter resizing, the capacity of the vector is "       << v.capacity() << " and its size is " << v.size();    vector<double>( v ).swap( v );    cout << "\nAfter swapping, the capacity of the vector is "       << v.capacity() << " and its size is " << v.size(); }