// L-10 MCS 360 Wed 15 Sep 2010 : vector_of_names.cpp

/* A vector of strings is used to collect names. 

   The interactive program below illustrates
   (1) the push_back() method to add an element to the end;
   (2) an iterator to write all elements of the vector;
   (3) the pop_back() method erases the last element;
   (4) the insert() method allows to insert an element anywhere;
   (5) the erase() method deletes an element anywhere in the vector. 

Note that the insert and erase require an iterator argument. */

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

void write ( vector<string> v );
// writes the vector v of names

void write_with_iterator ( vector<string> v );
// uses iterator to write elements of v

int main()
{
   vector<string> names;

   cout << "pushing names to the back..." << endl;
   do
   {
      string s;
      cout << "give a name : ";
      getline(cin,s,'\n');
      if(s == "") break;
      names.push_back(s);
   }
   while(true); 

   cout << "writing " << names.size()
        << " names..." << endl;
   write(names);
   cout << "using iterator to write..." << endl;
   write_with_iterator(names);

   if(names.size() > 0)
   {
      cout << "popping last element..." << endl;
      names.pop_back();
      write(names);
   }
   cout << "inserting a name..." << endl;
   string n;
   cout << "give a name : ";
   getline(cin,n,'\n');
   cout << "give an index : ";
   size_t i;
   cin >> i;
   names.insert(names.begin()+i,n);
   write(names);

   size_t k;
   cout << "give an index to delete : ";
   cin >> k;
   names.erase(names.begin()+k);
   write(names);

   vector<string> w = names;

   w[0] = "check for shallow copy";
   cout << names[0] << endl;
   cout << w[0] << endl;
   
   return 0;
}

void write ( vector<string> v )
{
   for(int i=0; i < v.size(); i++)
      cout << "v[" << i << "] = "
           << v[i] << endl;
}

void write_with_iterator ( vector<string> v )
{
   for(vector<string>::const_iterator item = v.begin();
       item != v.end(); item++)
      cout << *item << endl;
}

