[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. ...
随机推荐
- Quartz进一步学习与使用
一.再思考 了解Quartz.NET的基本使用方法了.但如果想方便的知道某个作业执行情况,需要暂停,启动等操作行为,这时候就需要个Job管理的界面,如何才能达到我们想到的效果,查看相关Quartz.n ...
- POJ 2104 K-th Number (可持久化线段树)
题目大意 给一个长度为n的序列,有m个询问,每次询问一个区间里面第k小的数. 解题分析 静态的区间第k大.复习了一下可持久化线段树. 首先对数值离散化,建一颗权值线段树.按照序列的顺序依次插入,每一个 ...
- Codeforces 799E(贪心)
题意: 有n个物品,购买物品i需要花费ci的代价.Arkady和Masha分别有喜欢的物品. 现在需要从中选m个,使得这m个物品中至少有k个Arkady喜欢的物品,k个Masha喜欢的物品. 输出满足 ...
- neutron dhcp
neutron dhcp 采用dnsmasq服务来实现.和传统的 dhcp 一样, dhcp请求也分为4步 The client sends a discover ("I'm a clien ...
- tsdb import 相关
今天一直在做opentsdb 大量导入数据的工作. 中间遇到了一些值得记录的问题, 这里随手记一下 明天好好整理 1. 多进程logger python的logging模块不支持多进程,但我们可以用s ...
- 使用maven时,如何修改JVM的配置参数;maven命令执行时到底消耗多少内存?
maven是使用java启动的,因此依赖JVM,那么如何修改JVM参数? MAVEN_OPTS 在系统的环境变量中,设置MAVEN_OPTS,用以存放JVM的参数,具体设置的步骤,参数示例如下: MA ...
- 如何使用TFTP客户端工具修复路由器固件
如何使用TFTP客户端工具修复路由器固件 编号:12083 来自:NetGear 更新日期:2013-10-14 访问数量:24650 NETGEAR无线路由器中, ...
- CentOS 5.5 介绍
转载:http://www.osyunwei.com/archives/3625.html 操作系统:CentOS 5.5主机名称:wwwIP地址:192.168.21.128网关:192.168.2 ...
- MySQL 高可用架构在业务层面的分析研究
)读多写少 虚线表示跨机房部署,比方电子商务系统.一个Master既有读也有些写.对读数据一致性须要比較重要的.读要放在Master上面. M(R)仅仅是一个备库.仅仅有M(WR)挂了之后,才会切换到 ...
- Storm计算结果是怎样存放的
Storm计算的结果存放在哪里? 刚開始接触Storm的时候.往往都会有这么一个疑问:"Storm处理后 的计算结果是保存在哪里呢?"是内存中还是在其他的地方? 官方给出的解释是: ...