C++STL——list
一、相关定义
list
- 链表,分配的内存不连续
- 可以高效地进行插入/删除元素
- 不可随机访问,访问速度慢
特征
- 只能通过迭代器来访问list中的元素
- 在头和尾都可以插入元素
二、list
【前提条件】
- #include<list>
- using std::list;
【迭代器】
- list<int>::iterator it;
【构造函数】
- 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() 返回指向链表最后一个元素之后的迭代器。
list<int> a1{1,2,3,4,5};
list<int>::iterator it;
for(it = a1.begin();it!=a1.end();it++){
cout << *it << "\t";
}
cout << endl;
c.rbegin() 返回逆向链表的第一个元素,即c链表的最后一个数据。
c.rend() 返回逆向链表的最后一个元素的下一个位置,即c链表的第一个数据再往前的位置。
list<int> a1{1,2,3,4,5};
list<int>::reverse_iterator it;
for(it = a1.rbegin();it!=a1.rend();it++){
cout << *it << "\t";
}
cout << endl;
operator= 重载赋值运算符
list<int> a1 {1,2,3,4,5},a2;
a2 = a1;
list<int>::iterator it;
for(it = a2.begin();it!=a2.end();it++){
cout << *it << endl;
}
c.assign(n,num) 将n个num拷贝赋值给链表c。
c.assign(beg,end) 将[beg,end)区间的元素拷贝赋值给链表c。
int a[5] = {1,2,3,4,5};
list<int> a1;
list<int>::iterator it;
a1.assign(2,10);
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
a1.assign(a,a+5);
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
c.front() 返回链表c的第一个元素。
c.back() 返回链表c的最后一个元素。
list<int> a1{1,2,3,4,5};
if(!a1.empty()){
cout << "the first number is:" << a1.front() << endl;
cout << "the last number is:" << a1.back() << endl;
}
c.empty() 判断链表是否为空。
list<int> a1{1,2,3,4,5};
if(!a1.empty())
cout << "a1 is not empty" << endl;
else
cout << " a1 is empty" << endl;
c.size() 返回链表c中实际元素的个数。
list<int> a1{1,2,3,4,5};
cout << a1.size() << endl;
c.max_size() 返回链表c可能容纳的最大元素数量。
list<int> a1{1,2,3,4,5};
cout << a1.max_size() << endl;
c.clear() 清除链表c中的所有元素。
list<int> a1{1,2,3,4,5};
list<int>::iterator it;
cout << "clear before:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << "\t";
}
cout << endl;
a1.clear();
cout << "clear after:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << "\t";
}
cout << endl;
c.insert(pos,num) 在pos位置插入元素num。
c.insert(pos,n,num) 在pos位置插入n个元素num。
c.insert(pos,beg,end) 在pos位置插入区间为[beg,end)的元素。
list<int> a1{1,2,3,4,5};
list<int>::iterator it;
cout << "insert before:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl; a1.insert(a1.begin(),0);
cout << "insert(pos,num) after:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl; a1.insert(a1.begin(),2,88);
cout << "insert(pos,n,num) after:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl; int arr[5] = {11,22,33,44,55};
a1.insert(a1.begin(),arr,arr+3);
cout << "insert(pos,beg,end) after:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
c.erase(pos) 删除pos位置的元素。
list<int> a1{1,2,3,4,5};
list<int>::iterator it;
cout << "erase before:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
a1.erase(a1.begin());
cout << "erase after:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
c.push_back(num) 在末尾增加一个元素。
c.pop_back() 删除末尾的元素。
c.push_front(num) 在开始位置增加一个元素。
c.pop_front() 删除第一个元素。
list<int> a1{1,2,3,4,5};
a1.push_back(10);
list<int>::iterator it;
cout << "push_back:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl; a1.pop_back();
cout << "pop_back:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl; a1.push_front(20);
cout << "push_front:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl; a1.pop_front();
cout << "pop_front:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
resize(n) 从新定义链表的长度,超出原始长度部分用0代替,小于原始部分删除。
resize(n,num) 从新定义链表的长度,超出原始长度部分用num代替。
list<int> a1{1,2,3,4,5};
a1.resize(8);
list<int>::iterator it;
cout << "resize(n):";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl; a1.resize(10, 10);
cout << "resize(n,num):";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
c1.swap(c2); 将c1和c2交换。
swap(c1,c2); 同上。
list<int> a1{1,2,3,4,5},a2,a3;
a2.swap(a1);
list<int>::iterator it;
cout << "a2.swap(a1):";
for(it = a2.begin();it!=a2.end();it++){
cout << *it << " ";
}
cout << endl; swap(a3,a2);
cout << "swap(a3,a2):";
for(it = a3.begin();it!=a3.end();it++){
cout << *it << " ";
}
return 0;
c1.merge(c2) 合并2个有序的链表并使之有序,从新放到c1里,释放c2。
c1.merge(c2,comp) 合并2个有序的链表并使之按照自定义规则排序之后从新放到c1中,释放c2。
c1.splice(c1.beg,c2) 将c2连接在c1的beg位置,释放c2
c1.splice(c1.beg,c2,c2.beg) 将c2的beg位置的元素连接到c1的beg位置,并且在c2中施放掉beg位置的元素
c1.splice(c1.beg,c2,c2.beg,c2.end) 将c2的[beg,end)位置的元素连接到c1的beg位置并且释放c2的[beg,end)位置的元素
remove(num) 删除链表中匹配num的元素。
list<int> a1{1,2,3,4,5};
a1.remove(3);
list<int>::iterator it;
cout << "remove():";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
reverse() 反转链表
list<int> a1{1,2,3,4,5};
a1.reverse();
list<int>::iterator it;
cout << "reverse:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
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;
重载运算符
operator==
operator!=
operator<
operator<=
operator>
operator>=
C++STL——list的更多相关文章
- 详细解说 STL 排序(Sort)
0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...
- STL标准模板库(简介)
标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...
- STL的std::find和std::find_if
std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...
- STL: unordered_map 自定义键值使用
使用Windows下 RECT 类型做unordered_map 键值 1. Hash 函数 计算自定义类型的hash值. struct hash_RECT { size_t operator()(c ...
- C++ STL简述
前言 最近要找工作,免不得要有一番笔试,今年好像突然就都流行在线笔试了,真是搞的我一塌糊涂.有的公司呢,不支持Python,Java我也不会,C有些数据结构又有些复杂,所以是时候把STL再看一遍了-不 ...
- codevs 1285 二叉查找树STL基本用法
C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...
- STL bind1st bind2nd详解
STL bind1st bind2nd详解 先不要被吓到,其实这两个配接器很简单.首先,他们都在头文件<functional>中定义.其次,bind就是绑定的意思,而1st就代表fir ...
- STL sort 函数实现详解
作者:fengcc 原创作品 转载请注明出处 前几天阿里电话一面,被问到STL中sort函数的实现.以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的,但听电话另一端面试官的声音,感觉不 ...
- STL的使用
Vector:不定长数组 Vector是C++里的不定长数组,相比传统数组vector主要更灵活,便于节省空间,邻接表的实现等.而且它在STL中时间效率也很高效:几乎与数组不相上下. #include ...
- [C/C++] C/C++延伸学习系列之STL及Boost库概述
想要彻底搞懂C++是很难的,或许是不太现实的.但是不积硅步,无以至千里,所以抽时间来坚持学习一点,总结一点,多多锻炼几次,相信总有一天我们会变得"了解"C++. 1. C++标准库 ...
随机推荐
- JS JavaScript闭包和作用域
JavaScript高级程序设计中对闭包的定义:闭包是指有权访问另外一个函数作用域中变量的函数. 从概念上,闭包有两个特点: 1.函数 2.能访问另外一个函数的作用域中的变量 在ES6之前,JavaS ...
- 课时91.CSS元素显示模式(掌握)
在HTML中HTML将所有的标签分为两类,分别是容器级和文本级 在CSS中CSS也将所有的标签分为两类,分别是块级元素和行内元素 1.什么是块级元素,什么是行内元素? 块级元素会独占一行 行内元素不会 ...
- CentOS 7设置网卡开机自动启用
一.查看网卡配置 root权限 [root@dbsyn ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue ...
- iOS 从0到1搭建高可用App框架
iOS 从0到1搭建高可用App框架 最近在搭建新项目的iOS框架,一直在思考如何才能搭建出高可用App框架,能否避免后期因为代码质量问题的重构.以前接手过许多“烂代码”,架构松散,底层混乱,缺少规范 ...
- hdu_2067_小兔的棋盘
小兔的叔叔从外面旅游回来给她带来了一个礼物,小兔高兴地跑回自己的房间,拆开一看是一个棋盘,小兔有所失望.不过没过几天发现了棋盘的好玩之处.从起点(0,0)走到终点(n,n)的最短路径数是C(2n,n) ...
- LeetCode 相交链表
基本思路 先计算出两个链表的长度 O(n) 将长的一个链表的指示指针移动到和短链表相同长度 O(n) 两个链表指示指针同时向前移动,直到二者相同或者NULL 代码实现 /** * Definition ...
- Linux命令备忘录:quota显示磁盘已使用的空间与限制
quota命令用于显示用户或者工作组的磁盘配额信息.输出信息包括磁盘使用和配额限制. 语法 quota(选项)(参数) 选项 -g:列出群组的磁盘空间限制: -q:简明列表,只列出超过限制的部分: - ...
- (数据科学学习手札24)逻辑回归分类器原理详解&Python与R实现
一.简介 逻辑回归(Logistic Regression),与它的名字恰恰相反,它是一个分类器而非回归方法,在一些文献里它也被称为logit回归.最大熵分类器(MaxEnt).对数线性分类器等:我们 ...
- 数据库 MySQL part2
表记录的操作 增 1.插入一条记录 语法:insert [into] tab_name (field1,filed2,.......) values (value1,value2,.......); ...
- Hadoop启动后无法启动NodeManager
在配置完Hadoop集群后,使用命令:“start-all.sh”进行启动集群.然后使用命令:“jps”查看进程启动情况,发现没有NodeManager 只需要使用命令:cd /usr/local/ ...