C++ //list容器 构造函数 //list赋值和交换 //list容器大小操作 //list插入和删除,移除 //清空 //list数据存取back(); front() //list 反转和排序

  1 //list容器 构造函数     //list赋值和交换   //list容器大小操作
  2 //list插入和删除,移除   //清空       //list数据存取back();  front()
  3 //list 反转和排序        
  4 #include<iostream>
  5 #include<list>
  6 #include<algorithm>
  7 
  8 using namespace std;
  9 
 10 
 11 //打印
 12 void printList(const list<int>&L)
 13 {
 14     for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
 15     {
 16         cout << *it << " ";
 17     }
 18     cout << endl;
 19 }
 20 
 21 
 22 //构造函数
 23 void test01()
 24 {
 25     list<int>L1;   //默认构造
 26 
 27     //添加数据
 28     L1.push_back(10);
 29     L1.push_back(20);
 30     L1.push_back(20);
 31     L1.push_back(40);
 32 
 33 
 34     //遍历容器
 35     printList(L1);
 36 
 37 
 38     //区间构造
 39     list<int>L2(L1.begin(), L1.end());
 40     printList(L2);
 41 
 42 
 43     //拷贝构造
 44     list<int>L3(L2);
 45     printList(L3);
 46 
 47     //n个elem
 48     list<int>L4(10, 222);
 49 
 50     printList(L4);
 51 }
 52 //打印
 53 void printList2(const list<int>& l2)
 54 {
 55     for (list<int>::const_iterator it = l2.begin(); it != l2.end(); it++)
 56     {
 57         cout << *it << " ";
 58     }
 59     cout << endl;
 60 }
 61 
 62 //list赋值和交换
 63 void test02()
 64 {
 65     list<int>L2;
 66 
 67     L2.push_back(10);
 68     L2.push_back(20);
 69     L2.push_back(30);
 70     L2.push_back(40);
 71 
 72     printList2(L2);
 73 
 74     //赋值操z做
 75     list<int>L3;
 76     L3 = L2;   //operator= 赋值
 77     printList2(L3);
 78 
 79     list<int>L4;
 80     L4.assign(L3.begin(), L3.end());
 81     printList2(L4);
 82 
 83     list<int>L5;
 84     L5.assign(10, 999);
 85     printList2(L5);
 86 
 87 
 88 }
 89 
 90 //交换
 91 void test03()
 92 {
 93     list<int>L2;
 94 
 95     L2.push_back(10);
 96     L2.push_back(20);
 97     L2.push_back(30);
 98     L2.push_back(40);
 99 
100     list<int>L6;
101     L6.assign(10, 555);
102 
103 
104     cout << "交换前:" << endl;
105     printList2(L2);
106     printList2(L6);
107 
108     L2.swap(L6);
109     //L6.swap(L2);
110     cout << "交换后:" << endl;
111     printList2(L2);
112     printList2(L6);
113 
114 }
115 
116 //list容器大小操作
117 void test04()
118 {
119     list<int>L7;
120     L7.push_back(10);
121     L7.push_back(20);
122     L7.push_back(30);
123     L7.push_back(40);
124 
125     printList2(L7);
126 
127     //判读是否为空
128     if (L7.empty())
129     {
130         cout << "L7为空!" << endl;
131     }
132     else
133     {
134         cout << "L7不为空!!" << endl;
135         cout << "L7的元素个数为: " << L7.size() << endl;
136     }
137 
138     //重新指定大小
139     L7.resize(10,5);
140     printList2(L7);
141 
142     L7.resize(2);
143     printList2(L7);
144 }
145 
146 //list 插入和删除
147 void test05()
148 {
149     list<int>L5;
150 
151     //尾插
152     L5.push_back(10);
153     L5.push_back(20);
154     L5.push_back(30);
155     
156 
157     //头插
158     L5.push_front(100);
159     L5.push_front(200);
160     L5.push_front(300);
161 
162     // 300  200  100  10  20  30
163     printList2(L5);
164 
165     //尾删
166     L5.pop_back();
167     //300 200 100  10  20
168     printList2(L5);
169 
170     L5.pop_front();
171     printList2(L5);
172     //200  100  10  20
173 
174     //insert插入
175     L5.insert(L5.begin(),888);
176     //888 200 100 10 20
177     printList2(L5);
178 
179     list<int>::iterator it = L5.begin();
180     L5.insert(++it, 9999);
181     //888 9999 200 100 10 20
182     printList2(L5);
183 
184     //删除
185     it = L5.begin();
186     L5.erase(it);
187     //9999 200  100  10 20 
188     printList2(L5);
189 
190     it = L5.begin();
191     L5.erase(++it);
192     //9999   100  10 20 
193     printList2(L5);
194 
195     
196     L5.push_back(66666);
197     L5.push_back(66666);
198     L5.push_back(66666);
199     L5.push_back(66666);
200     L5.push_back(66666);
201     //9999  100  10  20 66666 66666 66666 66666 66666
202     printList2(L5);
203 
204 
205 
206     //移除 remove
207     L5.remove(66666);
208     printList2(L5); 
209     //9999  100  10  20
210 
211 
212     //清空
213     L5.clear();
214     printList2(L5);
215 
216 }
217 //list数据存取
218 void test06()
219 {
220     list<int>L6;
221 
222     L6.push_back(10);
223     L6.push_back(20);
224     L6.push_back(30);
225     L6.push_back(40);
226 
227     //L6[0] 不可以用[]访问list容器中的元素
228 
229     //L6.at(0); 不可以用at方式访问list容器中的元素
230 
231     //原始是 list本质是链表 不是用连续线性空间储存数据 迭代器也是不支持随机访问的
232 
233 
234     cout << "第一个元素为:   " << L6.front() << endl;
235     cout << "最后一给元素为: " << L6.back() << endl;
236 
237     //验证迭代是不支持随机访问的
238     list<int>::iterator it = L6.begin();
239     
240     it++;
241     //it--;
242     //支持双向
243     //it = it + 1;   //不支持
244 
245      
246 }
247 
248 //list 反转和排序
249 void test07()
250 {
251     list<int>L7;
252     L7.push_back(25);
253     L7.push_back(15);
254     L7.push_back(95);
255     L7.push_back(55);
256     L7.push_back(85);
257 
258     printList2(L7);
259 
260     cout << "反转后: " << endl;
261     L7.reverse();
262     printList2(L7);
263 
264 
265 
266 }
267 bool myCompare(int v1 ,int v2)
268 {
269     //降序  就让第一个数 > 第二个数
270     return v1 > v2;
271 }
272 //排序
273 void test08()
274 {
275     list<int>L7;
276     L7.push_back(25);
277     L7.push_back(15);
278     L7.push_back(95);
279     L7.push_back(55);
280     L7.push_back(85);
281 
282     //排序
283     cout << "排序前: " << endl;
284     printList2(L7);
285 
286     //所有不支持随机访问迭代器的容器,不可以使用标准算法
287     //不支持随机访问的迭代器的容器,内部会提供对应一些算法
288     //sort(L7.begin(), L7.end());
289 
290     L7.sort(); //默认从小到大  升序
291     cout << "升序排序后: " << endl;
292     printList2(L7);
293 
294     //降序
295     cout << "降序排序后: " << endl;
296     L7.sort(myCompare);
297     printList2(L7);
298 }
299 
300 
301 int main()
302 {
303 
304     //test01();
305     //test02();
306     //test03();
307     //test04();
308 
309     // test05();
310 
311     //test06();
312 
313     test07();
314     test08();
315     system("pause");
316     return 0;
317 }

 

posted on 2021-08-15 15:10  Bytezero!  阅读(102)  评论(0编辑  收藏  举报