1: //search_n2a.cpp
3: #include <iostream>
4: #include <vector>
5: #include <algorithm>
6: using namespace std;
8: /**
9: Tests if two positive integers have the same digit sum.
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 digitSumsAreSame
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 search_n() "
39: "algorithm\n(extended version) to find instances of a consecutive "
40: "sequence of\nmatching values in a vector of integers, where "
41: "values match if and\nonly if they have the same digit sum.";
42: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
44: int a[] = {17, 27, 63, 54, 45, 71, 28, 81, 90, 18, 10, 9, 11};
45: vector<int> v(a, a+13);
46: cout << "\nHere are the contents of the vector:\n";
47: for (vector<int>::size_type i=0; i<v.size(); i++)
48: cout << v.at(i) << " ";
49: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
51: vector<int>::iterator p;
53: p = search_n(v.begin(), v.end(), 3, 9, digitSumsAreSame);
54: if (p != v.end())
55: cout << "\nThe first instance of three consecutive integers "
56: "\nwith a digit sum of 9 starts at location "
57: << (int)(p-v.begin()+1) << ".";
58: else
59: cout << "\nNo instance of three consecutive integers with a "
60: "\ndigit sum of 9 was found in the vector.";
61: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
63: p = search_n(p+1, v.end(), 3, 9, digitSumsAreSame);
64: if (p != v.end())
65: cout << "\nThe next instance of three consecutive integers "
66: "\nwith a digit sum of 9 starts at location "
67: << (int)(p-v.begin()+1) << ".";
68: else
69: cout << "\nNo further instance of three consecutive integers "
70: "\nwith a digit sum of 9 was found in the vector.";
71: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
73: p = search_n(p+1, v.end(), 3, 9, digitSumsAreSame);
74: if (p != v.end())
75: cout << "\nThe next instance of three consecutive integers "
76: "\nwith a digit sum of 9 starts at location "
77: << (int)(p-v.begin()+1) << ".";
78: else
79: cout << "\nNo further instance of three consecutive integers "
80: "\nwith a digit sum of 9 was found in the vector.";
81: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
83: p = search_n(p+1, v.end(), 3, 9, digitSumsAreSame);
84: if (p != v.end())
85: cout << "\nThe next instance of three consecutive integers "
86: "\nwith a digit sum of 9 starts at location "
87: << (int)(p-v.begin()+1) << ".";
88: else
89: cout << "\nNo further instance of three consecutive integers "
90: "\nwith a digit sum of 9 was found in the vector.";
91: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
92: }