1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| #include <list> #include <iostream> using namespace std; class MYTEST { public: void test1() { list<int> a{ 0,1,2,3,4,5 }; list<int> b{ 6,7,8,9,10 }; a.splice(a.end(), b); cout << "a 中的元素:"; for (auto& t : a) { cout << t << " "; } cout << endl; list<int> c{ 11,12,13 }; a.splice(a.end(), c, c.begin()); cout << "a 中的元素:"; for (auto& t : a) { cout << t << " "; } cout << endl; list<int> d{ 12,13,14,15 }; a.splice(a.end(), d, d.begin(), d.end()); cout << "a 中的元素:"; for (auto& t : a) { cout << t << " "; } cout << endl; cout << endl; } void test2() { list<int> a{ 0,1,2,3,4,5,5 }; a.remove(5);
cout << "a 中的元素:"; for (auto& t : a) { cout << t << " "; } cout << endl; cout << endl; } void test3() { list<int> a{ 0,1,2,3,4,5 }; auto pre = [](const int& a) { return a > 3; }; a.remove_if(pre);
cout << "a 中的元素:"; for (auto& t : a) { cout << t << " "; } cout << endl; cout << endl; } void test4() { list<int> a{ 0,1,2,3,4,5,5 }; a.unique(); cout << "a 中的元素:"; for (auto& t : a) { cout << t << " "; } cout << endl; auto binary_pre = [](const int& a, const int& b) { return (fabs(a - b) < 5); }; list<int> b{ 1,2,3,4,5,6,10,15 }; b.unique(binary_pre);
cout << "b 中的元素:"; for (auto& t : b) { cout << t << " "; } cout << endl; cout << endl; } void test5() { list<int> a{ 0,1,2,3,4,5 }; list<int> b{ 1,2,3,4,5,6 }; a.merge(b); cout << "a 中的元素:"; for (auto& t : a) { cout << t << " "; } cout << endl; list<double> c{ 1.4,2.2,2.9,3.1,3.7,7.1 }; list<double> d{ 2.1 }; auto cmp = [](const double& a, const double& b) { return (int(a) < int(b)); }; c.merge(d, cmp);
cout << "c 中的元素:"; for (auto& t : c) { cout << t << " "; } cout << endl; cout << endl; } void test6() { list<int> a{ 0,1,2,3,4,5 }; auto cmp = [](const int& a, const int& b) { return a > b; }; a.sort(cmp); cout << "a 中的元素:"; for (auto& t : a) { cout << t << " "; } cout << endl;
a.sort(); cout << "a 中的元素:"; for (auto& t : a) { cout << t << " "; } cout << endl; cout << endl; } void test7() { list<int> a{ 0,1,2,3,4,5 }; a.reverse();
cout << "a 中的元素:"; for (auto& t : a) { cout << t << " "; } cout << endl; cout << endl; } }; int main() { MYTEST a;
a.test1(); a.test2(); a.test3(); a.test4(); a.test5(); a.test6(); a.test7();
return 0; }
|