1: //list18.cpp 3: #include <iostream> 4: #include <list> 5: using namespace std; 7: bool hasSmallerDigitSum 8: ( 9: int i1, //in 10: int i2 //in 11: ) 12: /**< 13: Test if one integer has a smaller digit sum than another. 14: @return true if sum of the digits in i1 is < sum of digits 15: in i2, and otherwise returns false. 16: @param i1 An integer. 17: @param i2 An integer. 18: @pre i1 and i2 have been initialized and i1, i2 are both > 0. 19: @post No side effects. 20: */ 21: { 22: int digitSum1 = 0; 23: while (i1 != 0) 24: { 25: digitSum1 += i1 % 10; 26: i1 /= 10; 27: } 29: int digitSum2 = 0; 30: while (i2 != 0) 31: { 32: digitSum2 += i2 % 10; 33: i2 /= 10; 34: } 35: return digitSum1 < digitSum2; 36: } 38: int main() 39: { 40: cout << "\nThis program illustrates the merge() member function " 41: "for list objects."; 42: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 44: int a1[] = {1, 3, 5, 7, 9, 11, 13}; 45: list<int> lst1(a1, a1+7); 46: int a2[] = {2, 4, 6, 8, 10, 16, 20, 22}; 47: list<int> lst2(a2, a2+8); 49: cout << "\nFor the first list we have:"; 50: cout << "\nSize = " << lst1.size(); 51: cout << "\nContents: "; 52: list<int>::iterator p = lst1.begin(); 53: while (p!= lst1.end()) cout << *p++ << " "; 54: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 56: cout << "\nFor the second list we have:"; 57: cout << "\nSize = " << lst2.size(); 58: cout << "\nContents: "; 59: p = lst2.begin(); 60: while (p!= lst2.end()) cout << *p++ << " "; 61: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 63: cout << "\nNow we merge the second list with the first list."; 64: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 65: lst1.merge(lst2); 67: cout << "\nAfter the merge, for the first list we have:"; 68: cout << "\nSize = " << lst1.size(); 69: cout << "\nContents: "; 70: p = lst1.begin(); 71: while (p!= lst1.end()) cout << *p++ << " "; 72: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 74: cout << "\nAnd for the second list we now have:"; 75: cout << "\nSize = " << lst2.size(); 76: cout << "\nContents: "; 77: p = lst2.begin(); 78: while (p!= lst2.end()) cout << *p++ << " "; 79: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 81: cout << "\nNext we illustrate a merge in which the ordering of " 82: "the integer values is\ndetermined by the criterion that one " 83: "integer precedes another if and only\nif it has a smaller " 84: "digit sum."; 85: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 87: int a3[] = {20, 12, 15, 61, 17, 19}; 88: list<int> lst3(a3, a3+6); 89: int a4[] = {11, 23, 14, 81}; 90: list<int> lst4(a4, a4+4); 92: cout << "\nFor the first list we have:"; 93: cout << "\nSize = " << lst3.size(); 94: cout << "\nContents: "; 95: p = lst3.begin(); 96: while (p!= lst3.end()) cout << *p++ << " "; 97: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 99: cout << "\nFor the second list we have:"; 100: cout << "\nSize = " << lst4.size(); 101: cout << "\nContents: "; 102: p = lst4.begin(); 103: while (p!= lst4.end()) cout << *p++ << " "; 104: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 106: cout << "\nNow we merge the second list with the first list."; 107: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 108: lst3.merge(lst4, hasSmallerDigitSum); 110: cout << "\nAfter the merge, for the first list we have:"; 111: cout << "\nSize = " << lst3.size(); 112: cout << "\nContents: "; 113: p = lst3.begin(); 114: while (p!= lst3.end()) cout << *p++ << " "; 115: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 117: cout << "\nAnd for the second list we now have:"; 118: cout << "\nSize = " << lst4.size(); 119: cout << "\nContents: "; 120: p = lst4.begin(); 121: while (p!= lst4.end()) cout << *p++ << " "; 122: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 123: }