1: //mismatch2a.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 mismatch() " 39: "algorithm\n(extended version) to find mismatched values in two " 40: "ranges of\nintegers from two vectors of integers, when the sum " 41: "of digits\nis used to determine if two integer values match."; 42: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 44: int a1[] = {21, 13, 5}; 45: vector<int> v1(a1, a1+3); 46: cout << "\nHere are the contents of v1:\n"; 47: for (vector<int>::size_type i=0; i<v1.size(); i++) 48: cout << v1.at(i) << " "; 49: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 51: int a2[] = {21, 22, 111, 31, 24, 30, 31, 32}; 52: vector<int> v2(a2, a2+6); 53: cout << "\nHere are the contents of v2:\n"; 54: for (vector<int>::size_type i=0; i<v2.size(); i++) 55: cout << v2.at(i) << " "; 56: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 58: pair<vector<int>::iterator, vector<int>::iterator> mismatches; 60: cout << "\nNow we compare the sequence in v1 with the sequence " 61: "\nstarting at the third value in v2."; 62: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 64: mismatches = mismatch(v1.begin(), v1.end(), v2.begin()+2, 65: digitSumsAreSame); 66: if (mismatches.first == v1.end()) 67: cout << "\nThe two sequences match fully."; 68: else 69: cout << "\nThe first pair of mismatched values are\n" 70: << *mismatches.first << " from the first range, and\n" 71: << *mismatches.second << " from the second range."; 72: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 74: int a3[] = {21, 13, 5, 15, 61, 26, 18, 91}; 75: vector<int> v3(a3, a3+8); 76: cout << "\nHere are the contents of v3:\n"; 77: for (vector<int>::size_type i=0; i<v3.size(); i++) 78: cout << v3.at(i) << " "; 79: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 81: cout << "\nFinally, we compare the sequence in v1 with the " 82: "sequence in v3."; 83: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 85: mismatches = mismatch(v1.begin(), v1.end(), v3.begin(), 86: digitSumsAreSame); 87: if (mismatches.first == v1.end()) 88: { 89: cout << "\nThe two sequences match fully."; 90: cout << "\nThe second iterator of the pair returned points at\n" 91: << *mismatches.second << " in the second range (in v2)."; 92: } 93: else 94: cout << "\nThe first pair of mismatched values are\n" 95: << *mismatches.first << " from the first range, and\n" 96: << *mismatches.second << " from the second range."; 97: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 98: }