Source of lexicographical_compare2a.cpp


  1: //lexicographical_compare2a.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:         "lexicographical_compare() algorithm\n(extended version) to "
 40:         "test whether a range of integers in one vector precedes, "
 41:         "\nin the lexicographical (dictionary) sense, another range "
 42:         "of integers, in the\nsame vector, or in another vector. "
 43:         "In this case, the integer values are ordered\nin the sense "
 44:         "that one integer precedes another if and only if the digit "
 45:         "sum of\nthe first is less than the digit sum of the second.";
 46:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 48:     int a1[] = {21, 13, 7, 53, 46};
 49:     vector<int> v1(a1, a1+5);
 50:     cout << "\nHere are the contents of v1:\n";
 51:     for (vector<int>::size_type i=0; i<v1.size(); i++)
 52:         cout << v1.at(i) << " ";
 53:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 55:     int a2[] = {12, 22, 16, 35, 55, 1, 2};
 56:     vector<int> v2(a2, a2+7);
 57:     cout << "\nHere are the contents of v2:\n";
 58:     for (vector<int>::size_type i=0; i<v2.size(); i++)
 59:         cout << v2.at(i) << " ";
 60:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 62:     if (lexicographical_compare(v1.begin(), v1.end(),
 63:         v2.begin(), v2.end(), hasSmallerDigitSum))
 64:         cout << "\nv1 precedes v2, in dictionary order.";
 65:     else
 66:         cout << "\nv1 does not precede v2, in dictionary order.";
 67:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 69:     int a3[] = {12, 22, 16, 35, 54};
 70:     vector<int> v3(a3, a3+5);
 71:     cout << "\nHere are the contents of v3:\n";
 72:     for (vector<int>::size_type i=0; i<v3.size(); i++)
 73:         cout << v3.at(i) << " ";
 74:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 76:     if (lexicographical_compare(v1.begin(), v1.end(),
 77:         v3.begin(), v3.end(), hasSmallerDigitSum))
 78:         cout << "\nv1 precedes v3, in dictionary order.";
 79:     else
 80:         cout << "\nv1 does not precede v3, in dictionary order.";
 81:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 83:     int a4[] = {3, 4, 7, 8, 29};
 84:     vector<int> v4(a4, a4+5);
 85:     cout << "\nHere are the contents of v4:\n";
 86:     for (vector<int>::size_type i=0; i<v4.size(); i++)
 87:         cout << v4.at(i) << " ";
 88:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 90:     if (lexicographical_compare(v1.begin(), v1.end(),
 91:         v4.begin(), v4.end(), hasSmallerDigitSum))
 92:         cout << "\nv1 precedes v4, in dictionary order.";
 93:     else
 94:         cout << "\nv1 does not precede v4, in dictionary order.";
 95:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 97:     if (lexicographical_compare(v1.begin(), v1.begin()+3,
 98:         v1.begin()+3, v1.end(), hasSmallerDigitSum))
 99:         cout << "\nThe first three values of v1 "
100:             "precede the last three, in dictionary order.";
101:     else
102:         cout << "\nThe first three values of v1 "
103:             "do not precede the last three, in dictionary order.";
104:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
105: }