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>=
STL之list容器用法的更多相关文章
- STL:vector容器用法详解
vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组.像数组一样,vector类也用从0开始的下标表示元素的位置:但和数组不同的是,当vector对象创建后,数组的元素个数会随着ve ...
- STL——map/unordered_map基础用法
map /multimap map是STL里重要容器之一. 它的特性总结来讲就是:所有元素都会根据元素的键值key自动排序(也可根据自定义的仿函数进行自定义排序),其中的每个元素都是<key, ...
- STL中erase()的用法
erase()是STL提供的容器中比较常用的方法之一,它的功能是删除容器中的某些元素,其中它的函数原型如下: 1.有两个参数,且参数类型都是size_t型: string& erase ( s ...
- STL——关联式容器
一.关联式容器 标准的STL关联式容器分为set(集合)/map(映射表)两大类,以及这两大类的衍生体multiset(多键集合)和 multimap(多键映射表).这些容器的底层机制均以RB-tre ...
- STL中mem_fun, mem_fun_ref用法
1.引言 先看一个STL中for_each的用法: #include <iostream> #include <vector> #include <algorithm&g ...
- C++ STL 中 map 容器
C++ STL 中 map 容器 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它 ...
- C++ STL之list容器的基本操作
由于list和vector同属于序列式容器,有很多相同的地方,而上一篇中已经写了vector,所以这一篇着重写list和vector的不同之处和特有之处. 特别注意的地方: (1)STL中迭代器容器中 ...
- 带你深入理解STL之Vector容器
C++内置了数组的类型,在使用数组的时候,必须指定数组的长度,一旦配置了就不能改变了,通常我们的做法是:尽量配置一个大的空间,以免不够用,这样做的缺点是比较浪费空间,预估空间不当会引起很多不便. ST ...
- STL中的容器介绍
STL中的容器主要包括序列容器.关联容器.无序关联容器等. 一]序列容器 (1) vector vector 是数组的一种类表示,提供自动管理内存的功能,除非其他类型容器有更好满足程序的要求,否则,我 ...
随机推荐
- [USACO2005][POJ2454]Jersey Politics(随机化)
题目:http://poj.org/problem?id=2454 题意:给你3*k(k<=60)个数,你要将它们分成3个长度为k的序列,使得其中至少有两个序列的和大于k*500 分析:以为有高 ...
- cryptDB安装分析
cryptDB的安装脚步是用ruby语言写的,由于这里对ruby语言不熟悉,只能做简答的分析.我们先看看cryptDB的目录结构. 主要的目录有bins.doc.main.udf目录,下面我们通过分析 ...
- 第三十二课:JSDeferred的性能提速
大家如果看了前面两课,就知道Deferred的静态方法next(next_default)是用setTimeout实现的(有浏览器最小时钟间隔).但是实现这种异步操作,可以有很多种方法.JSDefer ...
- angular_$attrs
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 练习一_使用Git进行代码管理的心得
2015年9月19日,第一次软工实践课.助教给我们介绍了git,GitHub.显而易见,我并没有听懂.所以整个上午都在找教程,一个字一个字对着敲,然后敲着敲着就出错,回宿舍,继续敲,也是一样的... ...
- The web application [/codeMarket] registered the JBDC driver[.........] but failed to unregister it when the web application was stopped. To prevent
如果你报错了上面的这个严重,那么你的tomcat版本一定是在6.0.25之上的 原因:tomcat 6.025以后在sever.xml中引入了内存泄露侦测,对于垃圾回收不能处理的对像,它就会做日志. ...
- WordPress Option API(数据库储存 API)
WordPress Option API 是提供给开发者的数据库存储机制,通过调用函数,可以快速.安全的把数据存储到数据库里(都在 wp_options 表). 每个设置的模式是 key – valu ...
- Linux_日志信息
一.httpd日志:/var/log/httpd1.软件位置:whereis httpd2.配置文件位置:/etc/httpd/conf/httpd.conf 二.mysql日志:/var/log 查 ...
- DOM0,DOM2,DOM3事件,事件基础知识入门
事件是javascript和HTML交互基础, 任何文档或者浏览器窗口发生的交互, 都要通过绑定事件进行交互; 事件有DOM0, DOM2和DOM3的区分(别问我怎么少了一个DOM1, 也没找到DOM ...
- Html-Css-设置DIV边框圆滑
border-radius: 10px; -moz-border-radius: 10px; -webkit-border-radius: 10px; -o-border-radius: 10px; ...