#include <iostream>
#include <vector>

#include "misc.h"
// compile: $ C++ -I.. -o stl03 stl03.cpp

// Nigel Stewart 17th December 2001
template<class T,class A>
void shrink(std::vector<T,A> &v)
{
        std::vector<T,A> tmp;
        tmp.reserve(v.size());
        tmp.insert(tmp.begin(),v.begin(),v.end());
        v.swap(tmp);
}

// same as above (probably?)
template<class T>
void shrink2(vector<T> &v)
{
   vector<T> tmp(v.begin(),v.end());
   v.swap(tmp);
}


// Refer to reference "The C++ Standard Library, A tutorial
// and reference" page 149-150 in section 6.2.1 Abilites of
// Vectors, Size and Capacity


void test01()
{
   // Verifying NS's shrink function

   // on this system, vector grows in powers of 2

   vector<int> k;
   cout << SHOW(k.capacity()) << endl;
   //  k.capacity()=0

   k.push_back(5);
   cout << SHOW(k.capacity()) << endl;

   k.push_back(7);
   cout << SHOW(k.capacity()) << endl;

   k.push_back(51);
   cout << SHOW(k.capacity()) << endl;

   k.push_back(101);
   cout << SHOW(k.capacity()) << endl;

  k.push_back(107);
  cout << SHOW(k.capacity()) << endl;

  // here 5 elements, capacity of 8.  
  // Lets resize to remove waste

  shrink(k);

  cout << SHOW(k.capacity()) << endl;
}

void test02()
{
   int tmp[5] = {5,7,51,101,107};
   vector<int> k(tmp,tmp+5);
   cout << SHOW(k.capacity()) << endl;
   k.push_back(111);
   cout << SHOW(k.capacity()) << endl;

   cout << SHOW(k.size()) << endl;
   cout << "k.resize(k.size()) does not change \
the capacity!" << endl;

   k.resize(k.size()); 
   cout << SHOW(k.capacity()) << endl;

   // non-op
   k.reserve(k.size());
   cout << SHOW(k.capacity()) << endl;

   k.swap(k);
   cout << SHOW(k.capacity()) << endl;

   shrink2(k);
   cout << SHOW(k.capacity()) << endl;
}

void main()
{
   test02();
};




