C++进阶-3-5-list容器

  1 #include<iostream>
2 #include<list>
3 #include<algorithm>
4 using namespace std;
5
6 // list容器
7
8 void printList(const list<int>& L) {
9
10 for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
11 cout << *it << " ";
12 }
13 cout << endl;
14 }
15
16 // 1.构造函数
17 void test01() {
18
19 // 创建list容器
20 list<int> L1; // 默认构造
21
22 // 添加数据
23 L1.push_back(10);
24 L1.push_back(20);
25 L1.push_back(30);
26 L1.push_back(40);
27
28 // 遍历容器
29 printList(L1);
30
31
32 // 区间构造
33 list<int>L2(L1.begin(), L1.end());
34 printList(L2);
35
36 // 拷贝构造
37 list<int>L3(L2);
38 printList(L3);
39
40 //n个elem
41 list<int>L4(10, 200);
42 printList(L4);
43
44 }
45
46 // 2.赋值和交换
47 void test02() {
48
49 list<int> L1;
50
51 L1.push_back(10);
52 L1.push_back(20);
53 L1.push_back(30);
54 L1.push_back(40);
55
56 printList(L1);
57
58 // 赋值操作
59 list<int>L2;
60 L2 = L1;
61 printList(L2);
62
63 list<int>L3;
64 L3.assign(L2.begin(), L2.end());
65 printList(L3);
66
67 list<int>L4;
68 L4.assign(10, 100);
69 printList(L4);
70
71 // 交换操作
72 list<int>L5;
73 L5.assign(5, 22);
74
75 cout << "交换前:" << endl;
76 printList(L1);
77 printList(L5);
78
79 L1.swap(L5);
80 cout << "交换后:" << endl;
81 printList(L1);
82 printList(L5);
83
84 }
85
86 // 3.大小操作
87 void test03() {
88
89 list<int> L1;
90
91 L1.push_back(10);
92 L1.push_back(20);
93 L1.push_back(30);
94 L1.push_back(40);
95
96 printList(L1);
97
98 // 判断list容器是否为空
99 if (L1.empty()) {
100 cout << "L1为空" << endl;
101 }
102 else
103 {
104 cout << "L1不为空" << endl;
105 cout << "L1的元素个数为:" << L1.size() << endl;
106 }
107
108 // 重新指定大小
109 L1.resize(20);
110 printList(L1);
111
112 L1.resize(30, 22);
113 printList(L1);
114
115 L1.resize(4);
116 printList(L1);
117 }
118
119 // 4.插入和删除
120 void test04() {
121
122 list<int>L;
123
124 // 头插
125 L.push_front(10);
126 L.push_front(20);
127 L.push_front(30);
128
129 // 尾插
130 L.push_back(100);
131 L.push_back(200);
132 L.push_back(300);
133
134 printList(L);
135
136 // 头删
137 L.pop_front();
138
139 // 尾删
140 L.pop_back();
141
142 printList(L);
143
144 //insert插入
145 L.insert(L.begin(), 1000);
146 printList(L);
147
148 list<int>::iterator it = L.begin();
149 L.insert(++it, 345);
150 printList(L);
151
152 // 删除
153 it = L.begin();
154 L.erase(it);
155 printList(L);
156
157 // 移除
158 L.push_back(100000);
159 printList(L);
160 L.remove(100000); // 删除容器中所有与elem值匹配的元素
161 printList(L);
162
163 // 清空
164 L.clear();
165 printList(L);
166
167 }
168
169 // 5.数据存取
170 void test05() {
171
172 list<int> L1;
173
174 L1.push_back(10);
175 L1.push_back(20);
176 L1.push_back(30);
177 L1.push_back(40);
178
179 printList(L1);
180
181 // L[0]; // 不能用 [] 访问list容器中的元素
182 // L.at(0); // 不可以用 at 方式访问list容器中的元素
183 // 原因:list本质链表,不是用连续线性空间存储数据,迭代器也是不支持随机访问的
184
185 cout << "第一个元素是:" << L1.front() << endl;
186 cout << "最后一个元素是:" << L1.back() << endl;
187
188 // 验证迭代器是不支持随机访问的
189 list<int>::iterator it = L1.begin();
190 it++; // 支持双向
191 it--;
192 //it = it + 1; // 报错,不支持随机访问
193 }
194
195 // 6.反转和排序
196
197 bool myCompare(int v1, int v2) {
198 // 降序 就让第一个数 > 第二个数
199 return v1 > v2;
200 }
201
202 void test06() {
203
204 list<int> L1;
205
206 L1.push_back(30);
207 L1.push_back(20);
208 L1.push_back(60);
209 L1.push_back(40);
210 L1.push_back(10);
211
212 cout << "反转前:" << endl;
213 printList(L1);
214
215 // 反转
216 L1.reverse();
217 cout << "反转后:" << endl;
218 printList(L1);
219
220
221 // 排序,sort成员函数
222 cout << "排序前:" << endl;
223 printList(L1);
224
225 // 所有不支持随机访问迭代器的容器,不可以用标准的sort算法
226 //sort(L1.begin(), L1.end()); // 报错
227 // 不支持随机访问迭代器的容器,内部会提供对应的一些算法
228
229 L1.sort(); // 默认,升序
230
231 cout << "升序,排序后:" << endl;
232 printList(L1);
233
234 // 降序
235
236 L1.sort(myCompare);
237
238 cout << "降序,排序后:" << endl;
239 printList(L1);
240 }
241
242 // 7.排序案例,自定义数据类型
243 // 案例描述:
244 // 将Person自定义数据类型进行排序,Person中有属性:姓名,年龄,身高
245 // 排序规则:
246 // 按照年龄进行升序,如果年龄相同,按照身高进行降序
247
248 class Person {
249 public:
250 Person(string name, int age, int height) {
251 this->m_Name = name;
252 this->m_Age = age;
253 this->m_Height = height;
254 }
255
256 string m_Name;
257 int m_Age;
258 int m_Height;
259 };
260
261 // 比较自定义数据类型
262 bool comparePerson(Person& p1, Person& p2) {
263 if (p1.m_Age == p2.m_Age) {
264 return p1.m_Height > p2.m_Height;
265 }
266 else
267 {
268 return p1.m_Age < p2.m_Age;
269 }
270 }
271
272 void test07() {
273
274 // 创建容器
275 list<Person> L;
276
277 // 准备数据
278 Person p1("刘备", 35, 175);
279 Person p2("曹操", 45, 180);
280 Person p3("孙权", 40, 170);
281 Person p4("赵云", 25, 190);
282 Person p5("张飞", 35, 160);
283 Person p6("关羽", 35, 200);
284
285 // 插入数据
286 L.push_back(p1);
287 L.push_back(p2);
288 L.push_back(p3);
289 L.push_back(p4);
290 L.push_back(p5);
291 L.push_back(p6);
292
293 // 打印数据
294 for (list<Person>::iterator it = L.begin(); it != L.end(); it++) {
295 cout << "姓名:" << (*it).m_Name << " 年龄:" << it->m_Age << " 身高:" << it->m_Height << endl;
296 }
297
298 // 排序
299 cout << "-----------------------------" << endl;
300 cout << "排序后:" << endl;
301 L.sort(comparePerson);
302 for (list<Person>::iterator it = L.begin(); it != L.end(); it++) {
303 cout << "姓名:" << (*it).m_Name << " 年龄:" << it->m_Age << " 身高:" << it->m_Height << endl;
304 }
305
306 }
307
308
309 int main() {
310
311 // 1.构造函数
312 //test01();
313
314 // 2.赋值和交换
315 //test02();
316
317 // 3.大小操作
318 //test03();
319
320 // 4.插入和删除
321 //test04();
322
323 // 5.数据存取
324 //test05();
325
326 // 6.反转和排序,sort成员函数
327 //test06();
328
329 // 7.排序案例
330 test07();
331
332 system("pause");
333
334 return 0;
335 }
336
337 // 总结
338 //
339 // list 容器
340 //
341 // 功能:将数据进行链式存储
342 //
343 // 链表(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的
344 //
345 // list组成:链表由一系列节点组成
346 // 节点组成:一个存储数据元素的数据域,另一个是存储下一个节点地址的指针域
347 //
348 // STL中的链表是一个双向循环列表
349 //
350 // 优点:
351 // 1.可以对任意位置进行快速插入和删除元素
352 // 2.采用动态存储分配,不会造成内存浪费和已出
353 // 缺点:
354 // 1.空间(指针域)和时间(遍历)额外耗费较大
355 // 2.容器遍历速度,没有数组块
356 //
357 // 链表的存储空间并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
358 //
359 // List重要性质:插入、删除操作不会造成原有list迭代器的失效,这在vector是不成立的
360 //

C++进阶-3-5-list容器的更多相关文章

  1. Java进阶知识点:并发容器背后的设计理念

    一.背景 容器是Java编程中使用频率很高的组件,但Java默认提供的基本容器(ArrayList,HashMap等)均不是线程安全的.当容器和多线程并发编程相遇时,程序员又该何去何从呢? 通常有两种 ...

  2. 【c++进阶:c++ 顺序容器vector,string,deque,list,forward_list,array常用性质】

    常用5种顺序容器性质: https://blog.csdn.net/oil_you/article/details/82821833 关于deque https://www.cnblogs.com/L ...

  3. 容器进阶:OCI与容器运行时

    Blog:博客园 个人 什么是容器运行时(Container Runtime) Kubernetes节点的底层由一个叫做容器运行时的软件进行支撑,它负责比如启停容器 这样的事情.最广为人知的容器运行时 ...

  4. docker容器入门最佳教程

    为什么要写这个 简单回答是:容器技术非常热门,但门槛高. 容器技术是继大数据和云计算之后又一炙手可热的技术,而且未来相当一段时间内都会非常流行. 对 IT 行业来说,这是一项非常有价值的技术.而对 I ...

  5. 关于docker

    摘要: 最近很多阿里内部的同学和客户私信来咨询如何学习 Docker 技术.为此,我们列了一个路线图供大家学习Docker和阿里云容器服务.这个列表包含了一些社区的优秀资料和我们的原创文章.我们会随着 ...

  6. 2017年总结的前端文章——一劳永逸的搞定 flex 布局

    flex 基本概念 使用 flex 布局首先要设置父容器 display: flex,然后再设置 justify-content: center 实现水平居中,最后设置 align-items: ce ...

  7. 一劳永逸的搞定 FLEX 布局(转)

    一劳永逸的搞定 flex 布局 寻根溯源话布局 一切都始于这样一个问题:怎样通过 CSS 简单而优雅的实现水平.垂直同时居中.记得刚开始学习 CSS 的时候,看到 float 属性不由得感觉眼前一亮, ...

  8. 搞定flex布局

    这几种方式的搭配使用可以轻松搞定 PC 端页面的常见需求,比如实现水平居中可以使用 margin: 0 auto,实现水平垂直同时居中可以如下设置: .dad { position: relative ...

  9. 一劳永逸的搞定 flex 布局

    一劳永逸的搞定 flex 布局 寻根溯源话布局 一切都始于这样一个问题:怎样通过 CSS 简单而优雅的实现水平.垂直同时居中.记得刚开始学习 CSS 的时候,看到 float 属性不由得感觉眼前一亮, ...

  10. flex 布局学习

    flex 布局学习 寻根溯源话布局 一切都始于这样一个问题:怎样通过 CSS 简单而优雅的实现水平.垂直同时居中.记得刚开始学习 CSS 的时候,看到 float 属性不由得感觉眼前一亮,顺理成章的联 ...

随机推荐

  1. JavaScript 焦点事件

    焦点事件,当一个元素(比如链接或表单)得到或失去焦点时发生. 实例: 1 <!DOCTYPE html> 2 <html lang="en"> 3 < ...

  2. 学习Git(二)

    常用命令 git add 添加 git status 查看状态 git status -s 状态概览 git diff 对比 git diff --staged 对比暂存区 git commit 提交 ...

  3. instanceof关键字使用的方法(解决转型异常ClassCastException)

    一丶问题显现: 当你是父类的情况下,像使用子类的特定功能,就需要向下转型,但向下转型有可能会报错(ClassCastException) 而instanceof关键字就是解决异常的小能手,他能判断是否 ...

  4. Qunee for HTML5 v1.6新版本发布

    Qunee for HTML5 V1.6正式发布,修复了一些 BUG,增加了滚动条支持,改进了编辑器,增加了JSON 导入导出.告警冒泡.连线流动,UI 定制等扩展示例,欢迎 访问 导航面板 增加了滚 ...

  5. 关于disabled的事,你用对了吗?

    大家都知道disabled属性用来禁用表单里的元素.如果让你来禁用一个表单元素,你会怎么做么,下面提供几种写法,各位看官请看 html: <button type='button'>tes ...

  6. Html5 Canvas学习之路(五)

    Canvas 图像(上) Canvas 图像API可以加载图像数据,然后直接将图像应用到画布上.还可以裁切.拼贴图像数据,以显示用户需要的部分.此外,Canvas还提供了像素数据的存储功能,这样就能对 ...

  7. C#编写程序,计算数组中奇数之和和偶数之和

    编写程序,计算数组中奇数之和和偶数之和. 代码: using System; using System.Collections.Generic; using System.Linq; using Sy ...

  8. c++类调用的一个小问题

    先看这两段代码: #include <iostream> #include <vector> #include <algorithm> using namespac ...

  9. Android.mk文件如何打日志信息

    1. 在mk文件中添加:$(warning  "xxx="$(变量名)) 2. 执行lunch,选一个分支,此过程中可以打出添加的log.

  10. 关于页面中css某些情况下出现不知原因的隔断解决办法

    第一种方法:body{margin:0px;padding:0px position:absolute; top:0px;left:0px;} html{ width:100%; overflow-x ...