Source of set_symmetric_difference2a.cpp


  1: //set_symmetric_difference2a.cpp

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

  8: /**
  9: Tests if one integer has a smaller digit sum than another.
 10: Pre:\n i1 and i2 have been initialized and i1, i2 are both > 0.
 11: Post:\n Returns true if sum of the digits in i1 is < sum of digits
 12: in i2, and otherwise returns false.
 13: */
 14: bool hasSmallerDigitSum
 15: (
 16:     int i1, //in
 17:     int i2  //in
 18: )
 19: {
 20:     int digitSum1 = 0;
 21:     while (i1 != 0)
 22:     {
 23:         digitSum1 += i1 % 10;
 24:         i1 /= 10;
 25:     }

 27:     int digitSum2 = 0;
 28:     while (i2 != 0)
 29:     {
 30:         digitSum2 += i2 % 10;
 31:         i2 /= 10;
 32:     }
 33:     return digitSum1 < digitSum2;
 34: }

 36: int main()
 37: {
 38:     cout << "\nThis program illustrates the use of the STL "
 39:         "set_intersection() algorithm\n(extended version) to find the "
 40:         "values that are in a first vector of integers\nbut not in a "
 41:         "second vector of integers, as well as those integers that are "
 42:         "\nin the second vector but not in the first, and write them out "
 43:         "to a third\nvector of integers. In this case the vectors are "
 44:         "ordered in the sense that\none integer comes before another "
 45:         "if and only if it has a smaller digit sum.";
 46:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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

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

 56:     int a2[] = {2, 21, 21, 31, 31, 61, 71, 81};
 57:     vector<int> v2(a2, a2+8);

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

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

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

 73:     cout << "\nNow we find the values that are in v1 but not in v2, "
 74:         "as well as those that are\nin v2 but not in v1, and write "
 75:         "them out to v3, starting at the beginning of v3.";
 76:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 78:     vector<int>::iterator p;
 79:     p = set_symmetric_difference(v1.begin(), v1.end(),
 80:         v2.begin(), v2.end(), v3.begin(), hasSmallerDigitSum);

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

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