1 #include <iostream> 2 #include <list> 3 using namespace std; 4 5 int main() 6 { 7 int values[] = {1, 2, 3, 4}; 8 list<int> intList(values, values + 4); 9 10 cout << "Initial contents in intList: "; 11 for (int& e: intList) 12 cout << e << " "; 13 14 intList.assign(4, 11); 15 cout << "\nAfter the assign function, intList: "; 16 for (int& e: intList) 17 cout << e << " "; 18 19 auto itr = intList.begin(); 20 itr++; 21 intList.insert(itr, 555); 22 intList.insert(itr, 666); 23 cout << "\nAfter the insert function, intList: "; 24 for (int& e: intList) 25 cout << e << " "; 26 27 auto beg = intList.begin(); 28 itr++; 29 intList.erase(beg, itr); 30 cout << "\nAfter the erase function, intList: "; 31 for (int& e: intList) 32 cout << e << " "; 33 34 intList.clear(); 35 cout << "\nAfter the clear function, intList: "; 36 cout << "Size is " << intList.size() << endl; 37 cout << "Is empty? " << 38 (intList.empty() ? "true" : "false"); 39 40 intList.push_front(10); 41 intList.push_front(11); 42 intList.push_front(12); 43 cout << "\nAfter the push functions, intList: "; 44 for (int& e: intList) 45 cout << e << " "; 46 47 intList.pop_front(); 48 intList.pop_back(); 49 cout << "\nAfter the pop functions, intList: "; 50 for (int& e: intList) 51 cout << e << " "; 52 53 int values1[] = {7, 3, 1, 2}; 54 list<int> list1(values1, values1 + 4); 55 list1.sort(); 56 cout << "\nAfter the sort function, list1: "; 57 for (int& e: list1) 58 cout << e << " "; 59 60 list<int> list2(list1); 61 list1.merge(list2); 62 cout << "\nAfter the merge function, list1: "; 63 for (int& e: list1) 64 cout << e << " "; 65 cout << "\nSize of list2 is " << list2.size(); 66 67 list1.reverse(); 68 cout << "\nAfter the reverse function, list1: "; 69 for (int& e: list1) 70 cout << e << " "; 71 72 list1.push_back(7); 73 list1.push_back(1); 74 cout << "\nAfter the push functions, list1: "; 75 for (int& e: list1) 76 cout << e << " "; 77 78 list1.remove(7); 79 cout << "\nAfter the remove function, list1: "; 80 for (int& e: list1) 81 cout << e << " "; 82 83 list2.assign(7, 2); 84 cout << "\nAfter the assign function, list2: "; 85 for (int& e: list2) 86 cout << e << " "; 87 88 auto p = list2.begin(); 89 p++; 90 list2.splice(p, list1); 91 cout << "\nAfter the splice function, list2: "; 92 for (int& e: list2) 93 cout << e << " "; 94 cout << "\nAfter the splice function, list1’s size is " 95 << list1.size(); 96 97 return 0; 98 }