1: //list19.cpp 3: #include <iostream> 4: #include <list> 5: using namespace std; 7: int main() 8: { 9: cout << "\nThis program illustrates the splice() member function " 10: "for list objects."; 11: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 13: cout << "\nFirst we illustrate the splicing of an entire second list " 14: "into a first list."; 15: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 17: int a1[] = {1, 1, 1, 1, 1, 1, 1}; 18: list<int> lst1(a1, a1+7); 19: int a2[] = {2, 2, 2, 2, 2}; 20: list<int> lst2(a2, a2+5); 22: cout << "\nFor the first list we have:"; 23: cout << "\nSize = " << lst1.size(); 24: cout << "\nContents: "; 25: list<int>::iterator p = lst1.begin(); 26: while (p!= lst1.end()) cout << *p++ << " "; 27: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 29: cout << "\nFor the second list we have:"; 30: cout << "\nSize = " << lst2.size(); 31: cout << "\nContents: "; 32: p = lst2.begin(); 33: while (p!= lst2.end()) cout << *p++ << " "; 34: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 36: cout << "\nNow we splice the second list into the first list " 37: "just before the third value."; 38: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 39: list<int>::iterator pSplicePoint = lst1.begin(); 40: for (int i=1; i<=2; i++) ++pSplicePoint; 41: lst1.splice(pSplicePoint, lst2); 43: cout << "\nAfter the splice, for the first list we have:"; 44: cout << "\nSize = " << lst1.size(); 45: cout << "\nContents: "; 46: p = lst1.begin(); 47: while (p!= lst1.end()) cout << *p++ << " "; 48: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 50: cout << "\nAnd for the second list we now have:"; 51: cout << "\nSize = " << lst2.size(); 52: cout << "\nContents: "; 53: p = lst2.begin(); 54: while (p!= lst2.end()) cout << *p++ << " "; 55: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 57: cout << "\nNext we illustrate the splicing of a single value from " 58: "a second list\ninto a first list."; 59: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 61: int a3[] = {2, 1, 4, 3, 7, 6, 5}; 62: list<int> lst3(a3, a3+7); 63: int a4[] = {25, 22, 29, 23, 21}; 64: list<int> lst4(a4, a4+5); 66: cout << "\nFor the first list we have:"; 67: cout << "\nSize = " << lst3.size(); 68: cout << "\nContents: "; 69: p = lst3.begin(); 70: while (p!= lst3.end()) cout << *p++ << " "; 71: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 73: cout << "\nFor the second list we have:"; 74: cout << "\nSize = " << lst4.size(); 75: cout << "\nContents: "; 76: p = lst4.begin(); 77: while (p!= lst4.end()) cout << *p++ << " "; 78: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 80: cout << "\nNow we splice the 4th value from the second list into " 81: "the first list\njust before the 6th value."; 82: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 83: pSplicePoint = lst3.begin(); 84: for (int i=1; i<=5; i++) ++pSplicePoint; 85: list<int>::iterator pSpliceValue = lst4.begin(); 86: for (int i=1; i<=3; i++) ++pSpliceValue; 87: lst3.splice(pSplicePoint, lst4, pSpliceValue); 89: cout << "\nAfter the splice, for the first list we have:"; 90: cout << "\nSize = " << lst3.size(); 91: cout << "\nContents: "; 92: p = lst3.begin(); 93: while (p!= lst3.end()) cout << *p++ << " "; 94: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 96: cout << "\nAnd for the second list we now have:"; 97: cout << "\nSize = " << lst4.size(); 98: cout << "\nContents: "; 99: p = lst4.begin(); 100: while (p!= lst4.end()) cout << *p++ << " "; 101: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 103: cout << "\nFinallly we illustrate the splicing of a range of values " 104: "from a second list\ninto a first list."; 105: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 107: int a5[] = {1, 2, 3, 4, 5, 6, 7}; 108: list<int> lst5(a5, a5+7); 109: int a6[] = {21, 22, 23, 24, 25, 26, 27, 28}; 110: list<int> lst6(a6, a6+8); 112: cout << "\nFor the first list we have:"; 113: cout << "\nSize = " << lst5.size(); 114: cout << "\nContents: "; 115: p = lst5.begin(); 116: while (p!= lst5.end()) cout << *p++ << " "; 117: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 119: cout << "\nFor the second list we have:"; 120: cout << "\nSize = " << lst6.size(); 121: cout << "\nContents: "; 122: p = lst6.begin(); 123: while (p!= lst6.end()) cout << *p++ << " "; 124: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 126: cout << "\nNow we splice the 3rd to 7th values from the second list " 127: "into the first list\njust before the 5th value."; 128: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 129: pSplicePoint = lst5.begin(); 130: for (int i=1; i<=4; i++) ++pSplicePoint; 131: list<int>::iterator pSpliceBegin = lst6.begin(); 132: for (int i=1; i<=2; i++) ++pSpliceBegin; 133: list<int>::iterator pSpliceEnd = lst6.begin(); 134: for (int i=1; i<=7; i++) ++pSpliceEnd; 135: lst5.splice(pSplicePoint, lst6, pSpliceBegin, pSpliceEnd); 137: cout << "\nAfter the splice, for the first list we have:"; 138: cout << "\nSize = " << lst5.size(); 139: cout << "\nContents: "; 140: p = lst5.begin(); 141: while (p!= lst5.end()) cout << *p++ << " "; 142: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 144: cout << "\nAnd for the second list we now have:"; 145: cout << "\nSize = " << lst6.size(); 146: cout << "\nContents: "; 147: p = lst6.begin(); 148: while (p!= lst6.end()) cout << *p++ << " "; 149: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 150: }