1: //find_first_of2a.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 " 39: "find_first_of() algorithm (extended\nversion) to find the first " 40: "occurrence of any one of a range of integer values\nin a vector " 41: "within another range of integer values, also in a vector, and " 42: "where\ncorresponding values match if and only if they have the " 43: "same digit sum."; 44: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 46: int a1[] = {17, 5, 13, 21, 6, 7, 8, 14, 9, 10, 11}; 47: vector<int> v1(a1, a1+11); 48: cout << "\nHere are the contents of v1:\n"; 49: for (vector<int>::size_type i=0; i<v1.size(); i++) 50: cout << v1.at(i) << " "; 51: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 53: int a2[] = {12, 41, 19}; 54: vector<int> v2(a2, a2+3); 55: cout << "\nHere are the contents of v2:\n"; 56: for (vector<int>::size_type i=0; i<v2.size(); i++) 57: cout << v2.at(i) << " "; 58: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 60: vector<int>::iterator p; 62: p = find_first_of(v1.begin(), v1.end(), v2.begin(), v2.end(), 63: digitSumsAreSame); 64: if (p != v1.end()) 65: cout << "\nThe first instance of a value in v2 matching a " 66: "value in v1\nhappens at location " 67: << (int)(p-v1.begin()+1) << "."; 68: else 69: cout << "\nNo instance of a value in v2 matching a value " 70: "in v1 was found."; 71: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 73: p = find_first_of(p+1, v1.end(), v2.begin(), v2.end(), 74: digitSumsAreSame); 75: if (p != v1.end()) 76: cout << "\nThe next instance of a value in v2 matching a " 77: "value in v1\nhappens at location " 78: << (int)(p-v1.begin()+1) << "."; 79: else 80: cout << "\nNo further instance of a value in v2 matching a " 81: "value in v1 was found."; 82: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 84: p = find_first_of(p+1, v1.end(), v2.begin(), v2.end(), 85: digitSumsAreSame); 86: if (p != v1.end()) 87: cout << "\nThe next instance of a value in v2 matching a " 88: "value in v1\nhappens at location " 89: << (int)(p-v1.begin()+1) << "."; 90: else 91: cout << "\nNo further instance of a value in v2 matching a " 92: "value in v1 was found."; 93: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 95: p = find_first_of(p+1, v1.end(), v2.begin(), v2.end(), 96: digitSumsAreSame); 97: if (p != v1.end()) 98: cout << "\nThe next instance of a value in v2 matching a " 99: "value in v1\nhappens at location " 100: << (int)(p-v1.begin()+1) << "."; 101: else 102: cout << "\nNo further instance of a value in v2 matching a " 103: "value in v1 was found."; 104: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 105: }