Source of set_intersection1a.cpp


  1: //set_intersection1a.cpp

  3: #include <iostream>
  4: #include <vector>
  5: #include <algorithm>
  6: using namespace std;

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the use of the STL "
 11:         "set_intersection() algorithm\n(default version) to find the "
 12:         "values that are in a first vector of integers\nand also in a "
 13:         "second vector of integers, and write them out to a third "
 14:         "vector\nof integers.";
 15:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 17:     int a1[] = {11, 12, 12, 12, 12, 13, 14, 15};
 18:     vector<int> v1(a1, a1+8);

 20:     cout << "\nHere are the values in the vector v1:\n";
 21:     for (vector<int>::size_type i=0; i<v1.size(); i++)
 22:         cout << v1.at(i) << " ";
 23:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 25:     int a2[] = {11, 12, 12, 13, 13, 16, 17, 18};
 26:     vector<int> v2(a2, a2+8);

 28:     cout << "\nHere are the values in the vector v2:\n";
 29:     for (vector<int>::size_type i=0; i<v2.size(); i++)
 30:         cout << v2.at(i) << " ";
 31:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 33:     int a3[] = {101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
 34:                 111, 112, 113, 114, 115};
 35:     vector<int> v3(a3, a3+15);

 37:     cout << "\nHere are the values in the vector v3:\n";
 38:     for (vector<int>::size_type i=0; i<v3.size(); i++)
 39:         cout << v3.at(i) << " ";
 40:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 42:     cout << "\nNow we find the values that are in v1 and also in v2, "
 43:         "and\nwrite them out to v3, starting at the beginning of v3.";
 44:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 46:     vector<int>::iterator p;
 47:     p = set_intersection(v1.begin(), v1.end(),
 48:         v2.begin(), v2.end(), v3.begin());

 50:     cout << "\nHere are the revised contents of v3:\n";
 51:     for (vector<int>::size_type i=0; i<v3.size(); i++)
 52:         cout << v3.at(i) << " ";
 53:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 55:     cout << "\nThe iterator returned by the algorithm is pointing at "
 56:         "the value " << *p << ".";
 57:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 58: }