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. 200行代码,7个对象——让你了解ASP.NET Core框架的本质[3.x版]

    2019年1月19日,微软技术(苏州)俱乐部成立,我受邀在成立大会上作了一个名为<ASP.NET Core框架揭秘>的分享.在此次分享中,我按照ASP.NET Core自身的运行原理和设计 ...

  2. 复习笔记——1. C语言基础知识回顾

    1. 数据类型 1.1 基本数据类型 整型:int, long,unsigned int,unsigned long,long long-- 字符型:char 浮点型:float, double-- ...

  3. JAVA有关位运算的全套梳理

    一.在计算机中数据是如何进行计算的? 1.1:java中的byte型数据取值范围 我们最开始学习java的时候知道,byte类型的数据占了8个bit位,每个位上或0或1,左边第一位表示符号位,符号位如 ...

  4. PHP mysql事务问题实例分析

    本文实例分析了PHP的mysql事务问题.分享给大家供大家参考,具体如下: 对于myisam数据库,可以控制事务的进行: $mysqlrl = mysql_connect ( $db_config [ ...

  5. 助力SpringBoot自动配置的条件注解ConditionalOnXXX分析--SpringBoot源码(三)

    注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 前言 本篇接 如何分析SpringBoot源码模块及结构?--SpringBoot源码(二) 上一篇分析了SpringBoo ...

  6. django验证码框架captcha

    1.安装 2.在settings.py 安装app中添加 3.添加url 4.运行makemigrations和migrate 5.运用 在form表单中定义 view中返回form表单 在前端htm ...

  7. centos7下pymysql安装

    1. 安装 添加mysql yum respository 添加 MySQL Yum Repository 到你的系统 repository 列表中,执行 wget http://repo.mysql ...

  8. HTML5&CCS3(2) 处理网页文件

    2.1 规划网站 为什么要创建这个站点,需要展示的内容是什么? 应该如何调整内容使之吸引期望的访问者? 需要多少个页面?网站的结构是怎样? 为页面.图像和其他外部文件设计一个简单且一致的命名规则. 2 ...

  9. Kaggle 题目 nu-cs6220-assignment-1

    Kaggle题目 nu-cs6220-assignment-1 题目地址如下: https://www.kaggle.com/c/nu-cs6220-assignment-1/overview 这是个 ...

  10. 手把手教你用Abp vnext构建API接口服务

    ABP是一个开源应用程序框架,该项目是ASP.NET Boilerplate Web应用程序框架的下一代,专注于基于ASP.NET Core的Web应用程序开发,也支持开发控制台应用程序. 官方网站: ...