//************************************************************************
// A C++ program to provide examples of the use of vectors with
// the C++ standard template library
//
// Author: Sherry Towers
//         smtowers@asu.edu
// Created: Feb 13th, 2013
//
// Copyright Sherry Towers, 2013
//
// This program is not guaranteed to be free of bugs and/or errors.
//
// This program can be freely used and shared, as long as the author information
// and copyright in the header remains intact.
//************************************************************************
#include <iostream>
#include <vector>   // need this to use vectors
#include <numeric>  // need this for various operations on vectors, like summing
using namespace std;

//************************************************************************
//************************************************************************
//************************************************************************
int main (){
  vector<double> vt;  // creates an empty vector
  cout << endl;
  cout << "The size of the vt vector is " << vt.size() << endl;
  cout << endl;

  double delta_t = 1.0;
  double t = 0.0;
  while(t<(4.0)){
     vt.push_back(t);  // push_back() appends to the end of the vector
     t = t + delta_t;
  }
  vt.push_back(-2.0);

  cout << "The size of the vt vector is " << vt.size() << endl;
  cout << "vt is: " << endl;
  for (int i=0;i<vt.size();i++){ // indexing of vectors starts at 0
     cout << vt[i] << " ";
  }
  cout << endl;
  cout << endl;

  double sumvt = accumulate(vt.begin(),(vt.end()),0.0,plus<double>());
  double mysum = 0.0;
  for (int i=0;i<vt.size();i++){
     mysum = mysum + vt[i];
  }
  cout << "The sum of vt is " << sumvt << " and " << mysum << endl;
  cout << endl;

  int iind = max_element(vt.begin(),vt.end())-vt.begin();
  cout << "The index and value of the maximum is " 
       << iind << " " << vt[iind] << endl;
  cout << endl;
 
  int jind = min_element(vt.begin(),vt.end())-vt.begin();
  cout << "The index and value of the minimum is " 
       << jind << " " << vt[jind] << endl;
  cout << endl;

  vector<double> vtb;
  vtb = vt;
  transform(vt.begin()
           ,vt.end()
           ,vtb.begin()
           ,bind2nd(multiplies<double>(), 2.0)
           );
  cout << "vt*2 is: " << endl;
  for (int i=0;i<vtb.size();i++){ 
     cout << vtb[i] << " ";
  }
  cout << endl;
  cout << endl;

  vector<double> vtc;
  vtc = vt;
  vtc.insert((vtc.end()),vtb.begin(),vtb.end());
  cout << "vtc=(vt*2 appended to vt) is: " << endl;
  for (int i=0;i<vtc.size();i++){ 
     cout << vtc[i] << " ";
  }
  cout << endl;
  cout << endl;

  // now remove the third element from vtc
  vtc.erase(vtc.begin()+2);
  cout << "vtc with the third element removed is: " << endl;
  for (int i=0;i<vtc.size();i++){ 
     cout << vtc[i] << " ";
  }
  cout << endl;
  cout << endl;
  
  // now erase all elements from vt
  vt.clear();
  cout << "The length of the cleared vt vector is " << vt.size() << endl << endl;

  return 0;
}

