STL之list函数解析

list是C++标准模版库(STL,Standard Template Library)中的部分内容。实际上,list容器就是一个双向链表,可以高效地进行插入删除元素。

使用list容器之前必须加上头文件:#include;

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;

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) 自定义回调函数实现自定义排序

list<int> a1{1,3,2,5,4};
a1.sort();
list<int>::iterator it;
cout << "sort():";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl; a1.sort([](int n1,int n2){return n1>n2;});
cout << "sort(function point):";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;

STL之list函数解析的更多相关文章

  1. C++STL中的unique函数解析

    一.总述 unique函数属于STL中比较常用函数,它的功能是元素去重.即”删除”序列中所有相邻的重复元素(只保留一个).此处的删除,并不是真的删除,而是指重复元素的位置被不重复的元素给占领了(详细情 ...

  2. C++ STL - queue常见函数使用解析

    C++ STL - queue常见函数使用解析 c++队列模板类的定义在头文件中,queue 模板类需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque ...

  3. [转]javascript eval函数解析json数据时为什加上圆括号eval("("+data+")")

    javascript eval函数解析json数据时为什么 加上圆括号?为什么要 eval这里要添加 “("("+data+")");//”呢?   原因在于: ...

  4. 3.2 STL中的函数对象类模板

    *: STL中有一些函数对象类模板,如下所示: 1)例如要求两个double类型的x 和y 的积,可以: multiplies<double>()(x,y); 该表达式的值就是x*y的值. ...

  5. 使用STL库sort函数对vector进行排序

    使用STL库sort函数对vector进行排序,vector的内容为对象的指针,而不是对象. 代码如下 #include <stdio.h> #include <vector> ...

  6. PHP json_decode 函数解析 json 结果为 NULL 的解决方法

    在做网站 CMS 模块时,对于模块内容 content 字段,保存的是 json 格式的字符串,所以在后台进行模块内容的编辑操作 ( 取出保存的数据 ) 时,需要用到 json_decode() 函数 ...

  7. STL区间成员函数及区间算法总结

    STL区间成员函数及区间算法总结 在这里总结下可替代循环的区间成员函数和区间算法: 相比单元素遍历操作,使用区间成员函数的优势在于: 1)更少的函数调用 2)更少的元素移动 3)更少的内存分配 在区间 ...

  8. Matlab中bsxfun和unique函数解析

    一.问题来源 来自于一份LSH代码,记录下来. 二.函数解析 2.1 bsxfun bsxfun是一个matlab自版本R2007a来就提供的一个函数,作用是”applies an element-b ...

  9. socket使用TCP协议时,send、recv函数解析以及TCP连接关闭的问题

    Tcp协议本身是可靠的,并不等于应用程序用tcp发送数据就一定是可靠的.不管是否阻塞,send发送的大小,并不代表对端recv到多少的数据. 在阻塞模式下, send函数的过程是将应用程序请求发送的数 ...

随机推荐

  1. vue相关坑

    1:vue 动态加载图片路径报错解决方法,循环遍历图片不显示图片 解决方法:https://www.cnblogs.com/qingcui277/p/8930507.html

  2. Educational Codeforces Round 83 (Rated for Div. 2)A--C

    题意:给出一个边数为n的等边多边形,问是否可以变成m的等边多边形.条件是同一个中心,共用原顶点. 解析:直接n%m==0即可,这样就是平分了.签到题没得说了. #include<iostream ...

  3. 安装archlinux的另辟蹊径的命令及心得

    先说说我为什么开始入坑archlinux的吧,我最喜欢这个系统的一点就是简洁,DIY程度高,可以定制真正属于自己的专用系统.(像gentoo的话,就为了日常使用也没必要那么折腾,除非你是想在折腾的过程 ...

  4. arm 添加 ftp server 之 bftpd

    本来想装vsftp 结果装上以后执行报错 Segmentation fault , 换到几个 其它的小型ftp server 软件 ,试了 Stupid-FTPd,不能用. bftpd 可以使用,Ti ...

  5. JavaFX之FXML+CSS创建窗体以及透明窗体添加阴影

    前言 开通博客园有一段日子了,一直没空也没想好该写点什么.最近正好在做一个桌面程序,初次接触JavaFX,体验下来确实比swing好用不少.索性便记记学习笔记吧,虽然FX好像挺没存在感,没人用的感觉. ...

  6. 数据结构 5 哈希表/HashMap 、自动扩容、多线程会出现的问题

    上一节,我们已经介绍了最重要的B树以及B+树,使用的情况以及区别的内容.当然,本节课,我们将学习重要的一个数据结构.哈希表 哈希表 哈希也常被称作是散列表,为什么要这么称呼呢,散列.散列.其元素分布较 ...

  7. 【摩天大楼平地起】基础篇 09 简述N种查找算法

    引言 在开始之前首先可以先思考一下假如没有查找算法会是什么情况?所有数据结构都需要全部遍历一遍,每次都一遍又一遍的查,从本质而言查找算法就是为了提高效率. 经过前人一代又一代的努力,目前的查找算法大致 ...

  8. python3:input() 函数

    一.知识介绍: 1.input() 函数,接收任意输入,将所有输入默认为字符串处理,并返回字符串类型: 2.可以用作文本输入,如用户名,密码框的值输入: 3.语法:input("提示信息:& ...

  9. H5页面,输入框的光标,如果页面上下滑动光标停留在页面上,除了输入框外,松手过了一段时间才跑回输入框里面

    有点类似这种情况 其中一个博主描述得比较详细,主要还有图 我是直接在App.vue主文件那里添加一下代码,主要是添加一个监听器,如果touchmove的时候就会触发让其失焦,就会消失那个光标,需要再次 ...

  10. SpringMVC框架——文件的上传与下载

    使用SpringMVC框架做个小练习,需求: 1.单个图片上传并显示到页面中: 2.多个图片上传并显示到页面中: 3.上传文件后下载文件: 1.pom.xml中添加依赖 <!-- 文件上传 -- ...