1: //partial_sort_copy2a.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: "partial_sort_copy() algorithm\n(extended version) to " 40: "order a range of values from a vector of integers of\nsize 12 " 41: "into the order determined by increasing digit sum, and copy as " 42: "many\nof them as will fit into a range within a second vector of " 43: "integers. The\ncontents of the initial vector are unchanged by " 44: "this action."; 45: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 47: int a1[] = {92, 21, 53, 84, 46, 13, 41, 76, 45, 61, 11, 15}; 48: vector<int> v1(a1, a1+12); 49: cout << "\nHere are the initial contents of vector v1:\n"; 50: for (vector<int>::size_type i=0; i<v1.size(); i++) 51: cout << v1.at(i) << " "; 52: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 54: int a2[] = {100, 200, 300, 400, 500}; 55: vector<int> v2(a2, a2+5); 56: cout << "\nHere are the initial contents of vector v2:\n"; 57: for (vector<int>::size_type i=0; i<v2.size(); i++) 58: cout << v2.at(i) << " "; 59: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 61: cout << "\nNow we make the following call:"; 62: cout << "\np = partial_sort_copy(v1.begin(), v1.end(), " 63: "\n v2.begin(), v2.begin()+3, hasSmallerDigitSum);"; 64: vector<int>::iterator p; 65: p = partial_sort_copy(v1.begin(), v1.end(), 66: v2.begin(), v2.begin()+3, hasSmallerDigitSum); 67: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 69: cout << "\nAfter that call, here are the contents of v1:\n"; 70: for (vector<int>::size_type i=0; i<v1.size(); i++) 71: cout << v1.at(i) << " "; 72: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 74: cout << "\nAnd here are the contents of v2:\n"; 75: for (vector<int>::size_type i=0; i<v2.size(); i++) 76: cout << v2.at(i) << " "; 77: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 79: cout << "\nThe iterator p returned by the algorithm points to "; 80: if (p == v2.end()) 81: cout << "\nthe end of the ouput container."; 82: else 83: cout << "the value " << *p << "."; 84: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 86: cout << "\nNext we make the following call:"; 87: cout << "\np = partial_sort_copy(v1.begin()+2, v1.end()-2, " 88: "\n v2.begin(), v2.end(), hasSmallerDigitSum);"; 89: p = partial_sort_copy(v1.begin()+2, v1.end()-2, 90: v2.begin(), v2.end(), hasSmallerDigitSum); 91: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 93: cout << "\nAfter that call, here are the contents of v1:\n"; 94: for (vector<int>::size_type i=0; i<v1.size(); i++) 95: cout << v1.at(i) << " "; 96: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 98: cout << "\nAnd here are the contents of v2:\n"; 99: for (vector<int>::size_type i=0; i<v2.size(); i++) 100: cout << v2.at(i) << " "; 101: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 103: cout << "\nThe iterator p returned by the algorithm points to "; 104: if (p == v2.end()) 105: cout << "\nthe end of the ouput container."; 106: else 107: cout << "the value " << *p << "."; 108: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 109: }