/*
Removes an element from an ordered vector, i.e. the
ordering of the elements in the vector must be maintained.
v = a vector
pos = the position of the element to be erase
its value comes from keyboard user input
Algorithm:
Starting at position pos, replace it by the element below it
Repeat until the next to the last element.
Last element is then eliminated & the size of
the vector is decreased by one using pop_back.
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void erase(vector<string>& v, int pos);
// Precond: Vector must have at least 1 value.
// pos must be a non-negative int.
// postcond: Element at position pos is erased from vector.
// All elements below pos move up in position by 1.
// The size of the vector is then reduced by 1.
void print(vector<string> v);
// Precond: vector must have at least 1 value
// Postcond: content of vector printed to screen
// Purpose:
// Prints all elements in a vector.
// @param v = the vector to be printed
int main( )
{
vector<string> staff(5);
staff[0] = "Hacker, Harry";
staff[1] = "Reindeer, Rudolf";
staff[2] = "Cracker, Carl";
staff[3] = "Lam, Larry";
staff[4] = "Sandman, Susan";
print(staff);
int pos;
cout << "Remove which element? ";
cin >> pos;
erase(staff, pos);
print(staff);
return 0;
}
void print(vector<string> v)
{
for (unsigned i = 0; i < v.size( ); i++)
cout << "[" << i << "] " << v[i] << "\n";
}
void<.em> erase(vector<string>& v, int pos)
{
for (unsigned i = pos; i < v.size( ); i++) {
v[i] = v[i+1];
}
// reduce size of the vector by 1
v.pop_back( );
}