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 }

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

  1. cb14a_c++_顺序容器的操作7_赋值与交换(swap)_vector转list

    cb14a_c++_顺序容器的操作7_赋值与交换(swap) vector数据赋值给list, slist.assign(svec.begin(), svec.end());//这样可以转 svec- ...

  2. OOP3(继承中的类作用域/构造函数与拷贝控制/继承与容器)

    当存在继承关系时,派生类的作用域嵌套在其基类的作用域之内.如果一个名字在派生类的作用域内无法正确解析,则编译器将继续在外层的基类作用域中寻找该名字的定义 在编译时进行名字查找: 一个对象.引用或指针的 ...

  3. [c++基础]3/5原则--拷贝构造函数+拷贝赋值操作符

    /* * main.cpp * * Created on: Apr 7, 2016 * Author: lizhen */ #include <iostream> #include &qu ...

  4. C++类中函数(构造函数、析构函数、拷贝构造函数、赋值构造函数)

    [1]为什么空类可以创建对象呢? 示例代码如下: #include <iostream> using namespace std; class Empty { }; void main() ...

  5. C++学习基础六——复制构造函数和赋值操作符

    1.什么是复制构造函数 复制构造函数:是构造函数,其只有一个参数,参数类型是所属类的类型,且参数是一个const引用. 作用:将本类的成员变量赋值为引用形参的成员变量. 2.什么是赋值操作符 赋值操作 ...

  6. 深入理解c++构造函数, 复制构造函数和赋值函数重载(operator=)

    注 以下代码编译及运行环境均为 Xcode 6.4, LLVM 6.1 with GNU++11 support, Mac OS X 10.10.2 调用时机 看例子 // // main.cpp / ...

  7. C++复制构造函数和赋值符的区别

    From  http://blog.csdn.net/randyjiawenjie/article/details/6666937 非常感谢原作者分享. class CTest{public: CTe ...

  8. C++中的构造函数,拷贝构造函数和赋值运算

    关于C++中的构造函数,拷贝构造函数和赋值运算,以前看过一篇<高质量C++/C编程指南>的文章中介绍的很清楚,网上能搜索到,如果想详细了解这方面的知识可以参看一下这篇文章. 常见的给对象赋 ...

  9. 关于C++中的拷贝构造函数和赋值函数

    如果类定义的数据成员中存在指针或引用,那么最好重载这两个函数. 1.     定义 拷贝构造函数的定义格式:构造函数名(const 源类名& 引用对象形参名){} 赋值函数定义格式:源类名 & ...

  10. c++ 复制构造函数和赋值函数

    c++ 自动提供了下面这些成员函数 1默认构造函数 2.复制构造函数 3.赋值操作符 4.默认析构函数 5.地址操作符 赋值构造函数copy construtor 用于将一个对象复制到新创建的对象中, ...

随机推荐

  1. 【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析

    声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权,请联系我立即删除! 逆向目标 目标:某地公共资 ...

  2. 数字预失真(DPD)小试

    前言 射频功放的增益响应并非线性的,受到放大管饱和效应的影响,功放不可避免地出现非线性.甚至具有记忆效应的失真.这种非线性失真不仅产生高阶谐波,还会产生互调干扰,降低带内信噪比,影响带外信号.因此,需 ...

  3. SqlSugar跨库查询/多库查询

    一.跨库方式1:跨库导航 (5.1.3.24) 优点1:支持跨服务器,支持跨数据库品种, 支持任何类型数据库 优点2:   超级强大的性能,能达到本库联表性能 缺点:不支持子表过滤主表 (方案有ToL ...

  4. 【2】Visual Studio 2017同时配置OpenCV2.4 以及OpenCV4.3

    相关文章: [1]windows下安装OpenCV(4.3)+VS2017安装+opencv_contrib4.3.0配置 [2]Visual Studio 2017同时配置OpenCV2.4 以及O ...

  5. C++ Boost库 操作字符串与正则

    字符串的查找与替换一直是C++的若是,运用Boost这个准标准库,将可以很好的弥补C++的不足,使针对字符串的操作更加容易. 字符串格式转换: #include <iostream> #i ...

  6. 【C语言深度解剖】一篇解决程序的环境【编译+链接详解】让面试官给我们竖起大拇指

    文章目录 程序的翻译环境 翻译环境详解 编译 预编译 编译 汇编 关于形成符号表 链接 运行环境 尾声 [C语言深度解剖][Linux操作系统]程序的环境[编译+链接详解] 那么这里博主先安利一下一些 ...

  7. P9247 [集训队互测 2018] 完美的队列题解

    题目链接:[集训队互测 2018] 完美的队列 神仙数据结构题,看了很多题解才搞懂.在做此题之前,最好对分块很熟悉,对各类标记非常熟练.考虑题意说的种类是相对于全局的.我们可以考虑局部影响对全局影响. ...

  8. Sea-Search03总结&&un finish

    使用到的设计模式 Facade门面模式 为何使用? 在搜索项目中,由于使用Mvc架构且数据库中各种不同类型的数据源并没有放在同一张表,于是我们不可避免的在Controller中需要注入多个servic ...

  9. 案例:使用sqlplus登录报ORA-12547错误

    现象:Exadata刷机之后grid/oracle用户的环境变量是没有设置的,需要手工进行设置,设置完成后发现grid用户执行报错ORA-12547: [grid@dbm0dbadm01 ~]$ sq ...

  10. .Net Core + 微信赋能企业级智能客服系统--学习笔记

    摘要 围绕目前需求猛增的微信及移动端企业智能客服业务,利用 .NET Core 的一系列优秀特性及 SignalR 模块打造全双工.跨微信/QQ/钉钉等应用平台.跨系统平台.跨终端.支持企业级并发的移 ...