目录
push_front、push_back、pop_front、pop_back
list的介绍
前面我们已经学习过string以及vector,我们可以知道它们的元素是存放在连续的空间的(我们可以想象成数组),其优点是便于查找元素,但是当我们需要在中间插入或则删除时,就需要对元素进行大量的移动,这样就降低了程序的效率。
-
list是序列容器,允许在序列内任意位置进行恒定时间的插入和擦除操作,并实现双向迭代。
-
list以双链表的形式实现,可以将其所包含的每个元素存储在不同且无关的存储位置。
-
与forward_list非常相似:主要区别在于forward_list对象是单链表,因此只能向前迭代,以换取更小、更高效。
-
lists 和 forward_lists 的主要缺点是它们无法通过位置直接访问元素;例如,要访问列表中的第六个元素,必须从已知位置(如起点或终点)迭代到该位置,这在两者之间的距离上需要线性时间。它们还会消耗一些额外的内存,以保持与每个元素关联的关联信息(这对于大量小元素列表可能是一个重要因素)。
list的使用
list的定义

第一种无参构造:
cpp
list<int> l1;//无参构造
第二种构造一个包含n 个元素的容器。每个元素都是Val的复制品:
cpp
list<int> l2(10, 1);//10个1
第三种构造一个包含与值域[第一、最后]相同数量元素的容器,每个元素由该范围内对应元素构造,顺序相同
cpp
vector<int> v1(10, 1);
list<int> l3(v1.begin(), v1.end());//迭代器构造
第四种拷贝构造,构造一个容器,包含 x 中每个元素的副本,顺序相同
cpp
list<int> l4(l3);//拷贝构造
list的插入与删除
push_front、push_back、pop_front、pop_back
上面四个函数的功能是头插、尾插、头删、尾删:
cpp
void list_test4()
{
list<int> l1;
list<int> l2;
l1.assign(5, 1);
l2.assign(l1.begin(), l1.end());//这里相当于拷贝
for (auto it : l1)
{
cout << it << ' ';
}
cout << endl;
for (auto it : l2)
{
cout << it << ' ';
}
//头插尾插 头删尾删
l1.push_front(6);
l1.push_back(7);
l2.pop_front();
l2.pop_back();
cout << endl;
for (auto it : l1)
{
cout << it << ' ';
}
cout << endl;
for (auto it : l2)
{
cout << it << ' ';
}
}
insert
list当中的insert函数支持三种插入方式:
- 在指定迭代器位置插入一个数。
- 在指定迭代器位置插入n个值为val的数。
- 在指定迭代器位置插入一段迭代器区间(左闭右开)。
cpp
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
using namespace std;
int main()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
list<int>::iterator pos = find(lt.begin(), lt.end(), 2);
lt.insert(pos, 9); //在2的位置插入9
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //1 9 2 3
pos = find(lt.begin(), lt.end(), 3);
lt.insert(pos, 2, 8); //在3的位置插入2个8
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //1 9 2 8 8 3
vector<int> v(2, 7);
pos = find(lt.begin(), lt.end(), 1);
lt.insert(pos, v.begin(), v.end()); //在1的位置插入2个7
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //7 7 1 9 2 8 8 3
return 0;
}
注: find函数是头文件 "algorithm"当中的一个函数,该函数在指定迭代器区间寻找指定值的位置,并返回该位置的迭代器。
erase
list当中的erase函数支持两种删除方式:
删除指定迭代器位置的元素。
删除指定迭代器区间(左闭右开)的所有元素。
cpp
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
using namespace std;
int main()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
list<int>::iterator pos = find(lt.begin(), lt.end(), 2);
lt.erase(pos); //删除2
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //1 3 4 5
pos = find(lt.begin(), lt.end(), 4);
lt.erase(pos, lt.end()); //删除4及其之后的元素
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //1 3
return 0;
}
list的迭代器的使用
begin和end
通过begin函数可以得到容器中第一个元素的正向迭代器,通过end函数可以得到容器中最后一个元素的后一个位置的正向迭代器。
正向迭代器遍历容器:
cpp
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt(10, 2);
//正向迭代器遍历容器
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
it++;
}
cout << endl;
return 0;
}
rbegin和rend
通过rbegin函数可以得到容器中最后一个元素的反向迭代器,通过rend函数可以得到容器中第一个元素的前一个位置的反向迭代器。
反向迭代器遍历容器:
cpp
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt(10, 2);
//反向迭代器遍历容器
list<int>::reverse_iterator rit = lt.rbegin();
while (rit != lt.rend())
{
cout << *rit << " ";
rit++;
}
cout << endl;
return 0;
}
list的元素获取
front和back
front函数用于获取list容器当中的第一个元素,back函数用于获取list容器当中的最后一个元素。
cpp
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt;
lt.push_back(0);
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
cout << lt.front() << endl; //0
cout << lt.back() << endl; //4
return 0;
}
list的大小控制
size
size函数用于获取当前容器当中的元素个数。
resize
resize的两种情况:
- 当所给值大于当前的size时,将size扩大到该值,扩大的数据为第二个所给值,若未给出,则默认为容器所存储类型的默认构造函数所构造出来的值。
- 当所给值小于当前的size时,将size缩小到该值。
empty
empty函数用于判断当前容器是否为空。
clear
clear函数用于清空容器,清空后容器的size为0。
list的操作函数
sort
sort函数可以将容器当中的数据默认排为升序。
cpp
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt;
lt.push_back(4);
lt.push_back(7);
lt.push_back(5);
lt.push_back(9);
lt.push_back(6);
lt.push_back(0);
lt.push_back(3);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //4 7 5 9 6 0 3
lt.sort(); //默认将容器内数据排为升序
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //0 3 4 5 6 7 9
return 0;
}
splice
splice函数用于两个list容器之间的拼接,其有三种拼接方式:
- 将整个容器拼接到另一个容器的指定迭代器位置。
- 将容器当中的某一个数据拼接到另一个容器的指定迭代器位置。
- 将容器指定迭代器区间的数据拼接到另一个容器的指定迭代器位置。
cpp
void list_test7()
{
list<int> l1;
list<int> l2;
for (int i = 0;i < 10;i++)
{
l2.push_back(i);
l1.push_back(i + 10);
}
l1.splice(l1.begin(), l2);
for (auto it : l1)
{
cout << it << ' ';
}
cout << endl;
for (auto it : l2)//l2的已经全部剪切到l1了
{
cout << it << ' ';
}
cout << endl;
//还有别的用法,可以只剪切一个元素或者利用迭代器剪切部分元素
for (int i = 0;i < 6;i++)
{
l1.remove(i);
l1.remove(i + 7);
}
l1.splice(l1.begin(), l2);
for (auto it : l1)
{
cout << it << ' ';
}
cout << endl;
}
注意: 容器当中被拼接到另一个容器的数据在原容器当中就不存在了。(实际上就是将链表当中的指定结点拼接到了另一个容器当中)