Demonstrating the STL vector capacity and reserve functions

Started by aruljothi, Jun 11, 2009, 07:08 PM

Previous topic - Next topic

aruljothi



#include <iostream>
#include <cassert>
#include <vector>
using namespace std;

int main()
{
  int N = 10000; // size of vectors

  vector<int> vector1, vector2;

  for (int k = 0; k != N; ++k) {
    vector<int>::size_type cap = vector1.capacity();
    vector1.push_back(k);
    if (vector1.capacity() != cap)
      cout << "k: " << k << ", new capacity: " << vector1.capacity() << endl;
  }

  vector2.reserve(N);
  for (int k = 0; k != N; ++k) {
    vector<int>::size_type cap = vector2.capacity();
    vector2.push_back(k);
    if (vector2.capacity() != cap)
      cout << "k: " << k << ", new capacity: " << vector2.capacity() << "\n";
  }

  return 0;
}