[C++ STL] list使用详解
一、概述
list 由双向链表(doubly linked list)实现而成,元素也存放在堆中,每个元素都是放在一块内存中,他的内存空间可以是不连续的,通过指针来进行数据的访问,这个特点使得它的随机存取变得非常没有效率,因此它没有提供 [] 操作符的重载。但是由于链表的特点,它可以很有效率的支持任意地方的插入和删除操作。
二、定义及初始化
使用之前必须加相应容器的头文件:
#include <list> // list属于std命名域的,因此需要通过命名限定,例如using std::list;
定义的代码如下:
list<int> a; // 定义一个int类型的列表a
list<int> a(10); // 定义一个int类型的列表a,并设置初始大小为10
list<int> a(10, 1); // 定义一个int类型的列表a,并设置初始大小为10且初始值都为1
list<int> b(a); // 定义并用列表a初始化列表b
deque<int> b(a.begin(), ++a.end()); // 将列表a中的第1个元素作为列表b的初始值
除此之外,还可以直接使用数组来初始化向量:
int n[] = { 1, 2, 3, 4, 5 };
list<int> a(n, n + 5); // 将数组n的前5个元素作为列表a的初值
三、基本操作
3.1 容量函数
- 容器大小:
lst.size(); - 容器最大容量:
lst.max_size(); - 更改容器大小:
lst.resize(); - 容器判空:
lst.empty();
#include <iostream>
#include <list>
using namespace std;
int main(int argc, char* argv[])
{
list<int> lst;
for (int i = 0; i<6; i++)
{
lst.push_back(i);
}
cout << lst.size() << endl; // 输出:6
cout << lst.max_size() << endl; // 输出:357913941
lst.resize(0); // 更改元素大小
cout << lst.size() << endl; // 输出:0
if (lst.empty())
cout << "元素为空" << endl; // 输出:元素为空
return 0;
}
3.2 添加函数
- 头部添加元素:
lst.push_front(const T& x); - 末尾添加元素:
lst.push_back(const T& x); - 任意位置插入一个元素:
lst.insert(iterator it, const T& x); - 任意位置插入 n 个相同元素:
lst.insert(iterator it, int n, const T& x); - 插入另一个向量的 [forst,last] 间的数据:
lst.insert(iterator it, iterator first, iterator last);
#include <iostream>
#include <list>
using namespace std;
int main(int argc, char* argv[])
{
list<int> lst;
// 头部增加元素
lst.push_front(4);
// 末尾添加元素
lst.push_back(5);
// 任意位置插入一个元素
list<int>::iterator it = lst.begin();
lst.insert(it, 2);
// 任意位置插入n个相同元素
lst.insert(lst.begin(), 3, 9);
// 插入另一个向量的[forst,last]间的数据
list<int> lst2(5, 8);
lst.insert(lst.begin(), lst2.begin(), ++lst2.begin());
// 遍历显示
for (it = lst.begin(); it != lst.end(); it++)
cout << *it << " "; // 输出:8 9 9 9 2 4 5
cout << endl;
return 0;
}
3.3 删除函数
- 头部删除元素:
lst.pop_front(); - 末尾删除元素:
lst.pop_back(); - 任意位置删除一个元素:
lst.erase(iterator it); - 删除 [first,last] 之间的元素:
lst.erase(iterator first, iterator last); - 清空所有元素:
lst.clear();
#include <iostream>
#include <list>
using namespace std;
int main(int argc, char* argv[])
{
list<int> lst;
for (int i = 0; i < 8; i++)
lst.push_back(i);
// 头部删除元素
lst.pop_front();
// 末尾删除元素
lst.pop_back();
// 任意位置删除一个元素
list<int>::iterator it = lst.begin();
lst.erase(it);
// 删除[first,last]之间的元素
lst.erase(lst.begin(), ++lst.begin());
// 遍历显示
for (it = lst.begin(); it != lst.end(); it++)
cout << *it << " "; // 输出:3 4 5 6
cout << endl;
// 清空所有元素
lst.clear();
// 判断list是否为空
if (lst.empty())
cout << "元素为空" << endl; // 输出:元素为空
return 0;
}
3.4 访问函数
- 访问第一个元素:
lst.front(); - 访问最后一个元素:
lst.back();
#include <iostream>
#include <list>
using namespace std;
int main(int argc, char* argv[])
{
list<int> lst;
for (int i = 0; i < 6; i++)
lst.push_back(i);
// 访问第一个元素
cout << lst.front() << endl; // 输出:0
// 访问最后一个元素
cout << lst.back() << endl; // 输出:5
return 0;
}
3.5 其他函数
- 多个元素赋值:
lst.assign(int nSize, const T& x); // 类似于初始化时用数组进行赋值 - 交换两个同类型容器的元素:
swap(list&, list&); 或 lst.swap(list&); - 合并两个列表的元素(默认升序排列):
lst.merge(); - 在任意位置拼接入另一个list:
lst.splice(iterator it, list&); - 删除容器中相邻的重复元素:
lst.unique();
#include <iostream>
#include <list>
using namespace std;
int main(int argc, char* argv[])
{
// 多个元素赋值s
list<int> lst1;
lst1.assign(3, 1);
list<int> lst2;
lst2.assign(3, 2);
// 交换两个容器的元素
// swap(lst1, lst2); // ok
lst1.swap(lst2);
// 遍历显示
cout << "交换后的lst1: ";
list<int>::iterator it;
for (it = lst1.begin(); it!=lst1.end(); it++)
cout << *it << " "; // 输出:2 2 2
cout << endl;
// 遍历显示
cout << "交换后的lst2: ";
for (it = lst2.begin(); it != lst2.end(); it++)
cout << *it << " "; // 输出:1 1 1
cout << endl;
list<int> lst3;
lst3.assign(3, 3);
list<int> lst4;
lst4.assign(3, 4);
// 合并两个列表的元素
lst4.merge(lst3); // 不是简单的拼接,而是会升序排列
cout << "合并后的lst4: ";
for (it = lst4.begin(); it != lst4.end(); it++)
cout << *it << " "; // 输出:3 3 3 4 4 4
cout << endl;
list<int> lst5;
lst5.assign(3, 5);
list<int> lst6;
lst6.assign(3, 6);
// 在lst6的第2个元素处,拼接入lst5
lst6.splice(++lst6.begin(), lst5);
cout << "拼接后的lst6: ";
for (it = lst6.begin(); it != lst6.end(); it++)
cout << *it << " "; // 输出:6 5 5 5 6 6
cout << endl;
// 删除容器中相邻的重复元素
list<int> lst7;
lst7.push_back(1);
lst7.push_back(1);
lst7.push_back(2);
lst7.push_back(2);
lst7.push_back(3);
lst7.push_back(2);
lst7.unique();
cout << "删除容器中相邻的重复元素后的lst7: ";
for (it = lst7.begin(); it != lst7.end(); it++)
cout << *it << " "; // 输出:1 2 3 2
cout << endl;
return 0;
}
四、迭代器与算法
1. 迭代器
- 开始迭代器指针:
lst.begin(); - 末尾迭代器指针:
lst.end();// 指向最后一个元素的下一个位置 - 指向常量的开始迭代器指针:
lst.cbegin();// 意思就是不能通过这个指针来修改所指的内容,但还是可以通过其他方式修改的,而且指针也是可以移动的。 - 指向常量的末尾迭代器指针:
lst.cend(); - 反向迭代器指针,指向最后一个元素:
lst.rbegin(); - 反向迭代器指针,指向第一个元素的前一个元素:
lst.rend();
#include <iostream>
#include <list>
using namespace std;
int main(int argc, char* argv[])
{
list<int> lst;
lst.push_back(1);
lst.push_back(2);
lst.push_back(3);
cout << *(lst.begin()) << endl; // 输出:1
cout << *(--lst.end()) << endl; // 输出:3
cout << *(lst.cbegin()) << endl; // 输出:1
cout << *(--lst.cend()) << endl; // 输出:3
cout << *(lst.rbegin()) << endl; // 输出:3
cout << *(--lst.rend()) << endl; // 输出:1
cout << endl;
return 0;
}
2. 算法
- 遍历元素
list<int>::iterator it;
for (it = lst.begin(); it != lst.end(); it++)
cout << *it << endl;
- 元素翻转
#include <algorithm>
reverse(lst.begin(), lst.end());
- 元素排序
#include <algorithm>
sort(lst.begin(), lst.end()); // 采用的是从小到大的排序
// 如果想从大到小排序,可以采用先排序后反转的方式,也可以采用下面方法:
// 自定义从大到小的比较器,用来改变排序方式
bool Comp(const int& a, const int& b)
{
return a > b;
}
sort(lst.begin(), lst.end(), Comp);
五、总结
可以看到,list 与 vector、deque 的用法基本一致,除了以下几处不同:
- list 为双向迭代器,故不支持
it+=i; - list 不支持下标访问和at方法访问。
[C++ STL] list使用详解的更多相关文章
- STL bind1st bind2nd详解
STL bind1st bind2nd详解 先不要被吓到,其实这两个配接器很简单.首先,他们都在头文件<functional>中定义.其次,bind就是绑定的意思,而1st就代表fir ...
- C++ STL bitset 容器详解
C++ STL bitset 容器详解 本篇随笔讲解\(C++STL\)中\(bitset\)容器的用法及常见使用技巧. \(bitset\)容器概论 \(bitset\)容器其实就是个\(01\)串 ...
- 2.3 C++STL vector容器详解
文章目录 2.3.1 引入 2.3.2 代码实例 2.3.3 运行结果 总结 2.3.1 引入 vector 容器 动态数组 可变数组 vector容器 单口容器(尾部操作效率高) vector动态增 ...
- [GeekBand] STL 仿函数入门详解
本文参考文献::GeekBand课堂内容,授课老师:张文杰 :C++ Primer 11 中文版(第五版) page 37 :网络资料: 叶卡同学的部落格 http://www.leavesite. ...
- C++ STL之vector详解
转自http://blog.sina.com.cn/s/blog_9f1c0931010180cy.html Vectors vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作 ...
- C++ STL Binary search详解
一.解释 以前遇到二分的题目都是手动实现二分,不得不说错误比较多,关于返回值,关于区间的左闭右开等很容易出错,最近做题发现直接使用STL中的二分函数方便快捷还不会出错,不过对于没有接触过的同学,二分函 ...
- C++ STL之LIST详解A
List 容器 list是C++标准模版库(STL,Standard Template Library)中的部分内容.实际上,list容器就是一个双向链表,可以高效地进行插入删除元素. 使用list容 ...
- [C++ STL] set使用详解
一.set介绍: set容器内的元素会被自动排序,set与map不同,set中的元素即是键值又是实值,set不允许两个元素有相同的键值.不能通过set的迭代器去修改set元素,原因是修改元素会破坏se ...
- Qt迭代器(Java类型和STL类型)详解
迭代器为访问容器类里的数据项提供了统一的方法,Qt 有两种迭代器类:Java 类型的迭代器和 STL 类型的迭代器. 两者比较,Java 类型的迭代器更易于使用,且提供一些高级功能,而 STL 类型的 ...
- 2.2 C++STL string容器详解
文章目录 引言 2.2.1 string的特性 2.2.2 string用法理论 2.2.2.1 string构造函数 2.2.2.2 string赋值操作 2.2.2.3 string取值操作 2. ...
随机推荐
- codevs——2750 心系南方灾区
2750 心系南方灾区 时间限制: 1 s 空间限制: 2000 KB 题目等级 : 青铜 Bronze 题解 查看运行结果 题目描述 Description 现在我国南方正在承受百年 ...
- Ubuntu 16.04出现chmod: 无效模式:"a"的问题解决
命令: chmod a+x file1 提示:注意文件的类型,如果用在文件夹上是不行的,但是文件确实可以的.
- mybatis resultmap标签type属性什么意思
mybatis resultmap标签type属性什么意思? :就表示被转换的对象啊,被转换成object的类型啊 <resultMap id="BaseResultMap" ...
- Linux学习系列之lvs+keepalived
LVS简介 LVS介绍 LVS是Linux Virtual Server的缩写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统,属于4层负载均衡 ipvs和ipvsadm的关系 我们使用配置LV ...
- 【转】golang中的并行与并发
原文:http://blog.csdn.net/taohaoge/article/details/27970421 ------------------------------------------ ...
- 任务调度(三)——Timer的替代品ScheduledExecutorService简单介绍
先前的两篇博文<任务调度(一)--jdk自带的Timer>和<任务调度(二)--jdk自带的Timer 动态改动任务运行计划>中,简介了一下Timer,能够实现几本的功能.可是 ...
- Zabbix ---proxy 代理
Zabbix zabbix 官网 : https://www.zabbix.com/ 环境准备: 三台服务器: server 端: 192.168.206.6 proxy 端 : 192.168.2 ...
- Myeclipse的优化方法
近期在实习,公司给分配了新的电脑,可是不知道怎么弄得,总是弄得非常卡,没办法仅仅有自己好好整理一下电脑了,另外.为了提高编程的效率.顺便也把Myeclipse也优化了一下. 第一步: 取消自己主动va ...
- Linux系统编程_6_进程环境(C程序典型的存储空间)
1.八种结束Linux进程的方法: 五种正常终止方式: main函数返回: 调用exit: 调用_exit或_Exit 最后一个线程从其启动例程返回 最后一个线程调用pthread_exit 三种异常 ...
- Servlet之中文乱码问题【入门版】
请求数据的中文问题 1 post请求提交中文 get请求建议不提交中文(只是也有方法解决) 1.1post request.setCharacterEncoding("utf-8" ...