[转]STL之list容器详解
List 容器
list是C++标准模版库(STL,Standard Template Library)中的部分内容。实际上,list容器就是一个双向链表,可以高效地进行插入删除元素。
使用list容器之前必须加上<vector>头文件:#include<list>;
list属于std命名域的内容,因此需要通过命名限定:using std::list;也可以直接使用全局的命名空间方式:using namespace std;
构造函数
list<int> c0; //空链表
list<int> c1(3); //建一个含三个默认值是0的元素的链表
list<int> c2(5,2); //建一个含五个元素的链表,值都是2
list<int> c4(c2); //建一个c2的copy链表
list<int> c5(c1.begin(),c1.end()); ////c5含c1一个区域的元素[_First, _Last)。
成员函数
c.begin() 返回指向链表第一个元素的迭代器。
c.end() 返回指向链表最后一个元素之后的迭代器。
1 list<int> a1{1,2,3,4,5};
2 list<int>::iterator it;
3 for(it = a1.begin();it!=a1.end();it++){
4 cout << *it << "\t";
5 }
6 cout << endl;
c.rbegin() 返回逆向链表的第一个元素,即c链表的最后一个数据。
c.rend() 返回逆向链表的最后一个元素的下一个位置,即c链表的第一个数据再往前的位置。
1 list<int> a1{1,2,3,4,5};
2 list<int>::reverse_iterator it;
3 for(it = a1.rbegin();it!=a1.rend();it++){
4 cout << *it << "\t";
5 }
6 cout << endl;
operator= 重载赋值运算符。
1 list<int> a1 {1,2,3,4,5},a2;
2 a2 = a1;
3 list<int>::iterator it;
4 for(it = a2.begin();it!=a2.end();it++){
5 cout << *it << endl;
6 }
c.assign(n,num) 将n个num拷贝赋值给链表c。
c.assign(beg,end) 将[beg,end)区间的元素拷贝赋值给链表c。

1 int a[5] = {1,2,3,4,5};
2 list<int> a1;
3 list<int>::iterator it;
4 a1.assign(2,10);
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;
9 a1.assign(a,a+5);
10 for(it = a1.begin();it!=a1.end();it++){
11 cout << *it << " ";
12 }
13 cout << endl;

c.front() 返回链表c的第一个元素。
c.back() 返回链表c的最后一个元素。
1 list<int> a1{1,2,3,4,5};
2 if(!a1.empty()){
3 cout << "the first number is:" << a1.front() << endl;
4 cout << "the last number is:" << a1.back() << endl;
5 }
c.empty() 判断链表是否为空。
1 list<int> a1{1,2,3,4,5};
2 if(!a1.empty())
3 cout << "a1 is not empty" << endl;
4 else
5 cout << " a1 is empty" << endl;
c.size() 返回链表c中实际元素的个数。
1 list<int> a1{1,2,3,4,5};
2 cout << a1.size() << endl;
c.max_size() 返回链表c可能容纳的最大元素数量。
1 list<int> a1{1,2,3,4,5};
2 cout << a1.max_size() << endl;
c.clear() 清除链表c中的所有元素。

1 list<int> a1{1,2,3,4,5};
2 list<int>::iterator it;
3 cout << "clear before:";
4 for(it = a1.begin();it!=a1.end();it++){
5 cout << *it << "\t";
6 }
7 cout << endl;
8 a1.clear();
9 cout << "clear after:";
10 for(it = a1.begin();it!=a1.end();it++){
11 cout << *it << "\t";
12 }
13 cout << endl;

c.insert(pos,num) 在pos位置插入元素num。
c.insert(pos,n,num) 在pos位置插入n个元素num。
c.insert(pos,beg,end) 在pos位置插入区间为[beg,end)的元素。

1 list<int> a1{1,2,3,4,5};
2 list<int>::iterator it;
3 cout << "insert before:";
4 for(it = a1.begin();it!=a1.end();it++){
5 cout << *it << " ";
6 }
7 cout << endl;
8
9 a1.insert(a1.begin(),0);
10 cout << "insert(pos,num) after:";
11 for(it = a1.begin();it!=a1.end();it++){
12 cout << *it << " ";
13 }
14 cout << endl;
15
16 a1.insert(a1.begin(),2,88);
17 cout << "insert(pos,n,num) after:";
18 for(it = a1.begin();it!=a1.end();it++){
19 cout << *it << " ";
20 }
21 cout << endl;
22
23 int arr[5] = {11,22,33,44,55};
24 a1.insert(a1.begin(),arr,arr+3);
25 cout << "insert(pos,beg,end) after:";
26 for(it = a1.begin();it!=a1.end();it++){
27 cout << *it << " ";
28 }
29 cout << endl;

c.erase(pos) 删除pos位置的元素。

1 list<int> a1{1,2,3,4,5};
2 list<int>::iterator it;
3 cout << "erase before:";
4 for(it = a1.begin();it!=a1.end();it++){
5 cout << *it << " ";
6 }
7 cout << endl;
8 a1.erase(a1.begin());
9 cout << "erase after:";
10 for(it = a1.begin();it!=a1.end();it++){
11 cout << *it << " ";
12 }
13 cout << endl;

c.push_back(num) 在末尾增加一个元素。
c.pop_back() 删除末尾的元素。
c.push_front(num) 在开始位置增加一个元素。
c.pop_front() 删除第一个元素。

1 list<int> a1{1,2,3,4,5};
2 a1.push_back(10);
3 list<int>::iterator it;
4 cout << "push_back:";
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;
9
10 a1.pop_back();
11 cout << "pop_back:";
12 for(it = a1.begin();it!=a1.end();it++){
13 cout << *it << " ";
14 }
15 cout << endl;
16
17 a1.push_front(20);
18 cout << "push_front:";
19 for(it = a1.begin();it!=a1.end();it++){
20 cout << *it << " ";
21 }
22 cout << endl;
23
24 a1.pop_front();
25 cout << "pop_front:";
26 for(it = a1.begin();it!=a1.end();it++){
27 cout << *it << " ";
28 }
29 cout << endl;

resize(n) 从新定义链表的长度,超出原始长度部分用0代替,小于原始部分删除。
resize(n,num) 从新定义链表的长度,超出原始长度部分用num代替。

1 list<int> a1{1,2,3,4,5};
2 a1.resize(8);
3 list<int>::iterator it;
4 cout << "resize(n):";
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;
9
10 a1.resize(10, 10);
11 cout << "resize(n,num):";
12 for(it = a1.begin();it!=a1.end();it++){
13 cout << *it << " ";
14 }
15 cout << endl;

c1.swap(c2); 将c1和c2交换。
swap(c1,c2); 同上。

1 list<int> a1{1,2,3,4,5},a2,a3;
2 a2.swap(a1);
3 list<int>::iterator it;
4 cout << "a2.swap(a1):";
5 for(it = a2.begin();it!=a2.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;
9
10 swap(a3,a2);
11 cout << "swap(a3,a2):";
12 for(it = a3.begin();it!=a3.end();it++){
13 cout << *it << " ";
14 }
15 return 0;

c1.merge(c2) 合并2个有序的链表并使之有序,从新放到c1里,释放c2。
c1.merge(c2,comp) 合并2个有序的链表并使之按照自定义规则排序之后从新放到c1中,释放c2。

1 list<int> a1{1,2,3},a2{4,5,6};
2 a1.merge(a2);
3 list<int>::iterator it;
4 cout << "a1.merge(a2):";
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;
9
10 a2.merge(a1,[](int n1,int n2){return n1>n2;});
11 cout << "a2.merge(a1,comp):";
12 for(it = a2.begin();it!=a2.end();it++){
13 cout << *it << " ";
14 }
15 cout << endl;

c1.splice(c1.beg,c2) 将c2连接在c1的beg位置,释放c2

1 list<int> a1{1,2,3},a2{4,5,6};
2 a1.splice(a1.begin(), a2);
3 list<int>::iterator it;
4 cout << "a1.merge(a2):";
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;

c1.splice(c1.beg,c2,c2.beg) 将c2的beg位置的元素连接到c1的beg位置,并且在c2中施放掉beg位置的元素

1 list<int> a1{1,2,3},a2{4,5,6};
2 a1.splice(a1.begin(), a2,++a2.begin());
3 list<int>::iterator it;
4 cout << "a1.merge(a2):";
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;
9 return 0;

c1.splice(c1.beg,c2,c2.beg,c2.end) 将c2的[beg,end)位置的元素连接到c1的beg位置并且释放c2的[beg,end)位置的元素

1 list<int> a1{1,2,3},a2{4,5,6};
2 a1.splice(a1.begin(),a2,a2.begin(),a2.end());
3 list<int>::iterator it;
4 cout << "a1.merge(a2):";
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;
9 return 0;

remove(num) 删除链表中匹配num的元素。

1 list<int> a1{1,2,3,4,5};
2 a1.remove(3);
3 list<int>::iterator it;
4 cout << "remove():";
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;

remove_if(comp) 删除条件满足的元素,参数为自定义的回调函数。

1 list<int> a1{1,2,3,4,5};
2 a1.remove_if([](int n){return n<3;});
3 list<int>::iterator it;
4 cout << "remove_if():";
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;

reverse() 反转链表

1 list<int> a1{1,2,3,4,5};
2 a1.reverse();
3 list<int>::iterator it;
4 cout << "reverse:";
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;

unique() 删除相邻的元素

1 list<int> a1{1,1,3,3,5};
2 a1.unique();
3 list<int>::iterator it;
4 cout << "unique:";
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;
9 return 0;

c.sort() 将链表排序,默认升序
c.sort(comp) 自定义回调函数实现自定义排序

1 list<int> a1{1,3,2,5,4};
2 a1.sort();
3 list<int>::iterator it;
4 cout << "sort():";
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;
9
10 a1.sort([](int n1,int n2){return n1>n2;});
11 cout << "sort(function point):";
12 for(it = a1.begin();it!=a1.end();it++){
13 cout << *it << " ";
14 }
15 cout << endl;

[转]STL之list容器详解的更多相关文章
- 跟我一起学STL(2)——vector容器详解
一.引言 在上一个专题中,我们介绍了STL中的六大组件,其中容器组件是大多数人经常使用的,因为STL容器是把运用最广的数据结构实现出来,所以我们写应用程序时运用的比较多.然而容器又可以序列式容器和关联 ...
- STL之vector容器详解
vector 容器 vector是C++标准模版库(STL,Standard Template Library)中的部分内容.之所以认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单的说: ...
- [转]STL之vector容器详解
vector 容器 vector是C++标准模版库(STL,Standard Template Library)中的部分内容.之所以认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单的说: ...
- [转]STL之deque容器详解
Deque 容器 deque容器是C++标准模版库(STL,Standard Template Library)中的部分内容.deque容器类与vector类似,支持随机访问和快速插入删除,它在容器中 ...
- 2.8 C++STL set/multiset容器详解
文章目录 2.8.1 引入 2.8.2 代码示例 2.8.3 代码运行结果 2.8.4 对组pair的补充 代码实例 运行结果 总结 2.8.1 引入 set/multiset容器概念 set和mul ...
- 2.9 C++STL map/multimap容器详解
文章目录 2.9.1 引入 2.9.2 代码示例 map案列 multimap案列 2.9.3 代码运行结果 总结 2.9.1 引入 map相对于set区别,map具有键值和实值,所有元素根据键值自动 ...
- C++ STL bitset 容器详解
C++ STL bitset 容器详解 本篇随笔讲解\(C++STL\)中\(bitset\)容器的用法及常见使用技巧. \(bitset\)容器概论 \(bitset\)容器其实就是个\(01\)串 ...
- C++中的STL中map用法详解(转)
原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解 Map是STL的一个关联容器,它提供 ...
- [Spring学习笔记 1 ] Spring 简介,初步知识--Ioc容器详解 基本原理。
一.Spring Ioc容器详解(1) 20131105 1.一切都是Bean Bean可是一个字符串或者是数字,一般是一些业务组件. 粒度一般比较粗. 2.Bean的名称 xml配置文件中,id属性 ...
随机推荐
- [luoguP1029] 最大公约数和最小公倍数问题(数论)
传送门 一.暴力枚举(加了点优化) #include <cstdio> int x, y, ans; inline int gcd(int x, int y) { return !y ? ...
- RMI分布式议程服务学习
转自:http://6221123.blog.51cto.com/6211123/1112619 这里讲述的是基于JDK1.5的RMI程序搭建,更简单的说是一个 HelloWorld RMI. 1. ...
- hdu 2647拓扑排序 容器
#include<stdio.h> #include<queue> #include<vector> #include<iostream> using ...
- springboot 2.x 集成 drools 7.x
简介:springboot 2.0.4.RELEASE 集成 drools 7.11.0.Final 1.项目结构 2. pom.xml文件 <?xml version="1.0&qu ...
- codevs2449 骑士精神
题目描述 Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标 ...
- 【转】建立一个更高级别的查询 API:正确使用Django ORM 的方式
这个就比较深入啦... http://www.oschina.net/translate/higher-level-query-api-django-orm 结论: 在视图和其他高级应用中使用源生的O ...
- C#: 根据指定压缩比率压缩图片
直接上代码: /// <summary> /// 根据指定压缩比率压缩图片 /// </summary> /// <param name="original&q ...
- 条款39: 避免 "向下转换" 继承层次
基类指针不能调用派生类的独有的成员,即使基类指针指向派生类对象,因为编译器是根据指针的静态类型来确定调用对象在内存中占据的空间的.此时可以使用static_cast来转换,但不要这么做,因为向下转换难 ...
- Scope Is the Enemy of Success
 Scope Is the Enemy of Success Dave Quick SCopE REFERS To A pRojECT'S SizE. How much time, effort, ...
- 使用Tomcat的一些经验和心得
如今将使用Tomcat的一些经验和心得写到这里.作为记录和备忘.假设有朋友看到,也请指教. 1.首先是Tomcat的获取和安装. 获取当然得上Apache的官方站点下载,开源免费.并且带宽也足够.下载 ...