Using Vectors in C++

A vector behaves as a special array. Its size changes automatically as items are inserted and deleted and there are inbuilt routines for purposes such as sorting.

Compare the behaviour of a vector with that of a set on the previous page. The vector maintains the original order of items in the array from which it was constructed. Another difference is that an inserted duplicate in a vector is retained.

The example below shows:
  • initialisation of a vector from an array;
  • insertion of an item or of all the items in another vector;
  • searching for an item;
  • deletion of an item;
  • output of the contents by (1) the use of an iterator and (2) traditional array notation (colourVector1[i]);
  • sorting in alphabetical order.

A copy of the output follows the code.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main ()
{
  string colours[] = {"red", "orange", "yellow", "green", "blue"};
  string colours2[] = {"indigo", "violet", "white", "black"};
  vector<string> colourVector1 (colours, colours + 5);
  if (find(colourVector1.begin(), colourVector1.end(), "blue") != colourVector1.end())
  {
    cout << colourVector1[4] << " found in vector" << endl;
  }
  if (find(colourVector1.begin(), colourVector1.end(), "indigo") == colourVector1.end())
    cout << "indigo not found in vector" << endl;
  else
    cout << "indigo found in vector" << endl;
  colourVector1.insert(colourVector1.end(), "indigo");
  cout << "indigo inserted" << endl;
  if (find(colourVector1.begin(), colourVector1.end(), "indigo") == colourVector1.end())
    cout << "indigo not found" << endl;
  else
    cout << "indigo found" << endl;
  cout << "vector size: " << colourVector1.size() << endl;
  colourVector1.erase(find(colourVector1.begin(), colourVector1.end(), "orange"));
  cout << "deleting orange" << endl;
  cout << "size of vector now " << colourVector1.size() << endl;

  vector<string> colourVector2 (colours2, colours2 + 4);
  vector<string>::iterator it;
  cout << "colours in colourVector2:" << endl;
  for (it = colourVector1.begin(); it != colourVector1.end(); it++)
    cout << *it << " ";
  cout << endl;
  cout << "Final colourVector1 with appended colourVector2:" << endl;
  colourVector1.insert(colourVector1.end(), colourVector2.begin(), colourVector2.end() );
  for (it = colourVector1.begin(); it != colourVector1.end(); it++)
    cout << *it << " ";
  cout << endl << "size of colourVector1: " << colourVector1.size() << endl;
  sort(colourVector1.begin(), colourVector1.end());
  cout << "Colours sorted alphabetically: " << endl;
  for (int i = 0; i < colourVector1.size(); i++)
    cout << colourVector1[i] << " ";
  cout << endl;

  return 0;
}
    

Output:

blue found in vector
indigo not found in vector
indigo inserted
indigo found
vector size: 6
deleting orange
size of vector now 5
colours in colourVector2:
red yellow green blue indigo
Final colourVector1 with appended colourVector2:
red yellow green blue indigo indigo violet white black
size of colourVector1: 9
Colours sorted alphabetically:
black blue green indigo indigo red violet white yellow
Programming - a skill for life!

How to apply your knowledge of Pascal when learning C/C++