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;
重载运算符
operator==
operator!=
operator<
operator<=
operator>
operator>=
关注 - 0
粉丝 - 11
» 下一篇:STL之deque容器详解
STL 之 list 容器详解相关推荐
- STL 之 deque容器详解
Deque 容器 deque容器是C++标准模版库(STL,Standard Template Library)中的部分内容.deque容器类与vector类似,支持随机访问和快速插入删除,它在容器中 ...
- STL关联式容器详解
STL关联式容器类别 1. map 定义在 头文件中,使用该容器存储的数据,其各个元素的键必须是唯一的(即不能重复),该容器会根据各元素键的大小,默认进行升序排序(调用 std::less). 2. ...
- c语言stl模板,c/c++开发分享C++ 标准模板库 STL 顺序容器详解
c++ 标准模板库 stl 顺序容器 容器 顺序性 重复性 支持迭代器 vector 动态数组 无序 可重复 随机访问迭代器 deque 双向队列 无序 可重复 随机访问迭代器 list 双向链表 无 ...
- 读Java并发编程实践记录_原子性_锁_同步容器详解_任务执行
原子性: 单独的,不可分割的操作 不要使用过期状态值来决策当下的状态, 一定要先检查再执行(不检查, 将引发数据修改,丢失) 避免延迟初始化(懒加载: 先查看对象 == null, 然后new), 有 ...
- 【36C++STL-常用容器----5、stack容器详解】
文章目录 12.5 stack容器 12.5.1 stack基本概念 12.5.2 stack常用接口 12.5 stack容器 12.5.1 stack基本概念 概念:stack是一种先进后出(Fi ...
- 高并发之并发容器详解
高并发之并发容器详解 一.vector Vector 是矢量队列,它是JDK1.0版本添加的类.继承于AbstractList,实现了List, RandomAccess, Cloneable这些接口 ...
- STL —— multimap的用法详解
文章目录 multimap的基本性质 STL--multimap容器的用法 multimap容器的创建与初始化 multimap容器包含的成员方法 multimap容器大小 multimap容器中键值 ...
- Docker核心技术之容器详解
一.容器简介 容器(Container):容器是一种轻量级.可移植.并将应用程序进行的打包的技术,使应用程序可以在几乎任何地方以相同的方式运行 Docker将镜像文件运行起来后,产生的对象就是容器.容 ...
- 高并发编程_高并发编程系列:7大并发容器详解(附面试题和企业编程指南)...
不知道从什么时候起,在Java编程中,经常听到Java集合类,同步容器.并发容器,高并发编程成为当下程序员需要去了解掌握的技术之一,那么他们有哪些具体分类,以及各自之间的区别和优劣呢? 只有把这些梳理 ...
最新文章
- 蓝桥杯——说好的进阶之去反复元素的排列组合
- 理解同步异步、阻塞与非阻塞
- ccna学习指南笔记9
- vue通过监听实现相同路径的视图重新加载
- ajax请求目标地址,AJAX功能目标
- [转] yaml基础
- spark-submit提交参数说明以及与yarn-site.xml中参数的相互约束关系+spark运行架构图解(持续更新中)
- Windows漏洞:MS08-067远程代码执行漏洞复现及深度防御
- mysql教程多表查询_解析Mysql多表查询的实现
- L3-003. 社交集群-PAT团体程序设计天梯赛GPLT(并查集)
- 常用的ASCII码值
- Centos7配置阿里YUM源
- 金蝶K3系统定制国际销售日报表
- Portainer中文汉化
- Linux发行版之CentOS,Mandriva,Redhat,Fedora,SuSE,Debian,Ubuntu比较
- Linux下查看CPU、内存占用率
- CodeForces 643 D.Bearish Fanpages(set+multiset)
- 用winrar压缩工具切分文件和合并文件
- 拼多多关键词搜索采集商品数据接口,拼多多分类ID搜索采集商品销量接口,拼多多上货接口,拼多多商品列表API接口
- 安卓图片处理Picasso的解析使用