#include <vector>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
using namespace std;


void test01()
{
  cout << "Removing duplicate entries in a vector" << endl;

  vector<int> v;
  unsigned int const n=15;
  unsigned int const w=5;
  for (unsigned int i=0; i<n; ++i)
    v.push_back(rand() % w);
 
  for (unsigned int i=0; i<n; ++i)
    cout << v[i] << " ";
  cout << endl << endl;

  cout << "First sort the vector" << endl;
  sort(v.begin(),v.end());
  for (unsigned int i=0; i<n; ++i)
    cout << v[i] << " ";
  cout << endl << endl;


  cout << "Then apply unique which returns the last position" << endl;
  cout << "For vectors this is non-destructive." << endl;
  vector<int>:: iterator iend = unique(v.begin(),v.end());
  cout << "Erase to end of vector" << endl;
  v.erase(iend,v.end());
  unsigned int const n2 = v.size();
  cout << "n2=" << n2 << endl;
  for (unsigned int i=0; i<n2; ++i)
    cout << v[i] << " ";
  cout << endl << endl;

  cout << endl;
}


void test02()
{
  cout << "Removing duplicate entries in a list" << endl;

  list<int> lst;
  unsigned int const n=15;
  unsigned int const w=5;
  for (unsigned int i=0; i<n; ++i)
    lst.push_back(rand() % w);

  for (list<int>::iterator i=lst.begin();
    i!=lst.end(); ++i)
    cout << *i << " ";
  cout << endl << endl;
 
  lst.sort();
  lst.unique();

  for (list<int>::iterator i=lst.begin();
    i!=lst.end(); ++i)
    cout << *i << " ";
  cout << endl << endl;
}



int main(int argc, char** argv)
{
  test01();
  test02();


  return 0;
}


