文章目录
list的介绍
- list是一种可以在常数范围内
O(1)
在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代(双向迭代器)- 任意位置的插入和删除都是O(1) list的底层是双向带头循环链表
- list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立结点当中,在结点中通过指针指向其前一个元素和后一个元素,
- list与forward_list(单链表)非常相似,最主要的不同在于forward_list是单链表,只能进行单方向迭代,
- 与其他容器相比,list通常在任意位置进行插入、删除元素的执行效率更高,
- list和forward_list最大的缺陷是不支持在任意位置的随机访问,其次,list还需要一些额外的空间,以保存每个结点之间的关联信息(对于存储的类型较小元素来说这可能是一个重要的因素)
list的使用
list的构造方式
list() | 构造空的list |
---|---|
list (size_type n, const value_type& val = value_type()) | 构造的list中包含n个值为val的元素 |
list (const list& x) | 拷贝构造函数 |
list (InputIterator first, InputIterator last) | 用[first, last)区间中的元素构造list |
方式一: 构造一个某类型的空容器,
list<int> lt1; //构造int类型的空容器
方式二: 构造一个含有n个val的某类型容器,
list<int> lt2(10, 2); //构造含有10个2的int类型容器
方式三: 拷贝构造某类型容器的复制品,
list<int> lt3(lt2); //拷贝构造int类型的lt2容器的复制品
方式四: 使用迭代器拷贝构造某一段内容,
string s("hello world");
list<char> lt4(s.begin(),s.end()); //构造string对象某段迭代器区间的内容
方式五: 构造数组某段区间的内容,
int arr[] = { 1, 2, 3, 4, 5 };
int sz = sizeof(arr) / sizeof(int);
list<int> lt5(arr, arr + sz); //构造数组某段区间的复制品 ->本质是调用迭代器区间的构造函数
原生指针可以当做天然迭代器使用,本质上vector和string的迭代器就是原生指针
打印任意容器的方法
template<class Con>
void PrintContainer(const Con& c)
{
//取类模板的内嵌类型,类模板没有实例化,编译器不会去找
//前面加typename,告诉编译器这个是一个类型
typename Con::const_iterator it = c.begin();
while (it != c.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
int main()
{
vector<int> v(3, 2);
PrintContainer(v);
list<char> lt(10, 'x');
PrintContainer(lt);
}
list的插入和删除
push_front和pop_front
push_front函数用于头插一个数据,pop_front函数用于头删一个数据,
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt;
lt.push_front(0);
lt.push_front(1);
lt.push_front(2);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //2 1 0
lt.pop_front();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //1 0
return 0;
}
push_back和pop_back
push_back函数用于尾插一个数据,pop_back函数用于尾删一个数据,
#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);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //0 1 2 3
lt.pop_back();
lt.pop_back();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;//0 1
return 0;
}
insert
list当中的insert函数支持三种插入方式:
- 在指定迭代器位置插入一个数,
- 在指定迭代器位置插入n个值为val的数,
- 在指定迭代器位置插入一段迭代器区间(左闭右开)
#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”当中的一个函数,该函数在指定迭代器区间寻找指定值的位置,并返回该位置的迭代器,
插入是否会导致迭代器失效问题?
不会,list底层是双向循环链表,指针仍指向原来的节点,所以不会导致迭代器失效
int main()
{
int a[] = { 1,2,3 };
int sz = sizeof(a) / sizeof(a[0]);
list<int> lt(a,a+sz);
list<int>::iterator pos = find(lt.begin(), lt.end(), 3);//查找3位置
lt.insert(pos, 30);//在pos位置前插入30
PrintContainer(lt);
cout << *pos << endl;//3 pos仍指向原来的节点
}
erase
list当中的erase函数支持两种删除方式:
- 删除指定迭代器位置的元素,
- 删除指定迭代器区间(左闭右开)的所有元素,
#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底层是双向循环链表,指针仍指向原来的节点.但是该节点已经被删除了,该指针就是野指针,不能被使用
int main()
{
int a[] = { 1,2,3 };
int sz = sizeof(a) / sizeof(a[0]);
list<int> lt(a,a+sz);
list<int>::iterator pos = find(lt.begin(), lt.end(), 3);//查找3位置
lt.erase(pos);//删除pos位置的值
cout << *pos << endl;//报错
}
前面说过,此处大家可将迭代器暂时理解成类似于指针,迭代器失效即迭代器所指向的节点的无效,即该节点被删除了,
因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list其它位置的迭代器失效的,只有在删除时才会导致迭代器失效,并且失效的只是被删除节点的迭代器,其他迭代器不会受到影响
err程序:
auto it = l.begin();
while (it != l.end())
{
// erase()函数执行后,it所指向的节点已被删除,因此it迭代器无效,在下一次使用it时,必须先给其赋值
l.erase(it);
++it;
}
修改
auto it = l.begin();
while (it != l.end())
{
l.erase(it++);//写法2:it = l.erase(it);
}
list的迭代器使用
begin +end | 返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器 |
---|---|
rbegin +rend | 返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的reverse_iterator,即begin位置 |
begin和end
通过begin函数可以得到容器中第一个元素的正向迭代器,通过end函数可以得到容器中最后一个元素的后一个位置的正向迭代器,
正向迭代器遍历容器:
#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函数可以得到容器中第一个元素的前一个位置的反向迭代器,
反向迭代器遍历容器:
#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;
}
注意:
- begin与end为正向迭代器,对正向迭代器执行++操作,迭代器向后移动
- rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动
list的元素获取
front和back
front函数用于获取list容器当中的第一个元素,back函数用于获取list容器当中的最后一个元素,
#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函数用于获取当前容器当中的元素个数,
#include <iostream>
#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);
cout << lt.size() << endl; //4
return 0;
}
resize
resize的两种情况:
- 当所给值大于当前的size时,将size扩大到该值,扩大的数据为第二个所给值,若未给出,则默认为容器所存储类型的默认构造函数所构造出来的值,
- 当所给值小于当前的size时,将size缩小到该值,
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt(5, 3);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //3 3 3 3 3
lt.resize(7, 6); //将size扩大为7,扩大的值为6
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //3 3 3 3 3 6 6
lt.resize(2); //将size缩小为2
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //3 3
return 0;
}
empty
empty函数用于判断当前容器是否为空,
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt;
cout << lt.empty() << endl; //1
return 0;
}
clear
clear函数用于清空容器,清空后容器的size为0, 但是头结点(哨兵位)不会被清除
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt(5, 2);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //2 2 2 2 2
cout << lt.size() << endl; //5
lt.clear(); //清空容器
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //(无数据)
cout << lt.size() << endl; //0
return 0;
}
list的操作函数
sort
可以将容器当中的数据默认排为升序, 注意:这里的sort函数不是算法库的sort函数,而是list容器自带的sort函数
#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(); //默认将容器内数据排为升序 等价于:lt.sort(less<int>());
//如果想排降序: lt.sort(greater<int>())
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //0 3 4 5 6 7 9
return 0;
}
splice
splice函数用于两个list容器之间的拼接(数据转移),其有三种拼接方式:
- 将整个容器拼接到另一个容器的指定迭代器位置,
- 将容器当中的某一个数据拼接到另一个容器的指定迭代器位置,
- 将容器指定迭代器区间的数据拼接到另一个容器的指定迭代器位置,
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt1(4, 2);
list<int> lt2(4, 6);
lt1.splice(lt1.begin(), lt2); //将容器lt2拼接到容器lt1的开头
for (auto e : lt1)
{
cout << e << " ";
}
cout << endl; //6 6 6 6 2 2 2 2
list<int> lt3(4, 2);
list<int> lt4(4, 6);
lt3.splice(lt3.begin(), lt4, lt4.begin()); //将容器lt4的第一个数据拼接到容器lt3的开头
for (auto e : lt3)
{
cout << e << " ";
}
cout << endl; //6 2 2 2 2
list<int> lt5(4, 2);
list<int> lt6(4, 6);
lt5.splice(lt5.begin(), lt6, lt6.begin(), lt6.end()); //将容器lt6的指定迭代器区间内的数据拼接到容器lt5的开头
for (auto e : lt5)
{
cout << e << " ";
}
cout << endl; //6 6 6 6 2 2 2 2
return 0;
}
注意: 容器当中被拼接到另一个容器的数据在原容器当中就不存在了,(实际上就是将链表当中的指定结点拼接到了另一个容器当中)
remove
remove函数用于删除容器当中特定值的元素, 找到了就删(所有的都删除),没就不删
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt;
lt.push_back(1);
lt.push_back(4);
lt.push_back(3);
lt.push_back(3);
lt.push_back(2);
lt.push_back(2);
lt.push_back(3);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //1 4 3 3 2 2 3
lt.remove(3); //删除容器当中值为3的元素
for (auto e : lt)
{
cout << e << " ";//1 4 2 2
}
cout << endl;
return 0;
}
remove_if
remove_if函数用于删除容器当中满足条件的元素,
参数 可以是函数 : 一个返回值为bool的函数
#include <iostream>
#include <list>
using namespace std;
bool single_digit(const int& val)
{
return val < 10;
}
int main()
{
list<int> lt;
lt.push_back(10);
lt.push_back(4);
lt.push_back(7);
lt.push_back(18);
lt.push_back(2);
lt.push_back(5);
lt.push_back(9);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //10 4 7 18 2 5 9
lt.remove_if(single_digit); //删除容器当中值小于10的元素
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //10 18
return 0;
}
unique
unique函数用于删除容器当中连续的重复元素
进行去重
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt;
lt.push_back(1);
lt.push_back(4);
lt.push_back(3);
lt.push_back(3);
lt.push_back(2);
lt.push_back(2);
lt.push_back(3);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //1 4 3 3 2 2 3
lt.sort(); //将容器当中的元素排为升序
lt.unique(); //删除容器当中连续的重复元素
for (auto e : lt)
{
cout << e << " ";//1 2 3 4
}
cout << endl;
return 0;
}
注意: 若想使用unique函数做到真正的去重,还需在去重前对容器内元素进行排序, 先排序 ->然后去重
merge
merge函数用于将一个有序list容器合并到另一个有序list容器当中,使得合并后的list容器任然有序,(类似于归并排序) 被合并的那个list变为空(即作为参数的容器变为空)
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt1;
lt1.push_back(3);
lt1.push_back(8);
lt1.push_back(1);
list<int> lt2;
lt2.push_back(6);
lt2.push_back(2);
lt2.push_back(9);
lt2.push_back(5);
lt1.sort(); //将容器lt1排为升序
lt2.sort(); //将容器lt2排为升序
lt1.merge(lt2); //将lt2合并到lt1当中
cout << lt2.empty() << endl;//1 lt2容器变为空
for (auto e : lt1)
{
cout << e << " ";
}
cout << endl; //1 2 3 5 6 8 9
return 0;
}
reverse
reverse函数用于将容器当中元素的位置进行逆置,
#include <iostream>
#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);
//1 2 3 4 5
lt.reverse(); //将容器当中元素的位置进行逆置
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; //5 4 3 2 1
return 0;
}
assign
assign函数用于将新内容分配给容器,替换其当前内容,新内容的赋予方式有两种:
- 将n个值为val的数据分配给容器,
- 将所给迭代器区间当中的内容分配给容器,
#include <iostream>
#include <string>
#include <list>
using namespace std;
int main()
{
list<char> lt(3, 'a');
lt.assign(3, 'b'); //将新内容分配给容器,替换其当前内容
for (auto e : lt)
{
cout << e << " ";//b b b
}
cout << endl;
string s("hello world");
lt.assign(s.begin(), s.end()); //将新内容分配给容器,替换其当前内容
for (auto e : lt)
{
cout << e << " "; //h e l l o w o r l d
}
cout << endl;
return 0;
}
swap
swap函数用于交换两个容器的内容,
建议用容器的swap,不建议用库的swap,因为库的swap涉及3次深拷贝
两个容器的容量可以不同
库里面的swap函数
![image-20220217125242180](https://mangoimage.oss-cn-guangzhou.aliyuncs.com/image-20220217125242180.png)
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt1(5, 2);
list<int> lt2(4, 6);
lt1.swap(lt2); //交换两个容器的内容
for (auto e : lt1)
{
cout << e << " ";
}
cout << endl; //6 6 6 6
for (auto e : lt2)
{
cout << e << " ";
}
cout << endl; //2 2 2 2 2
return 0;
}
关于sort函数
sort的底层是快速排序,快速排序要求容器迭代器必须是随机迭代器,比如:快排需要三数取中优化,不支持随机访问,效率就不够高了
所以:sort(lt.begin(), lt.end())是不可行的, list底层是链表,三数取中效率低下.但是可以使用list自带sort函数进行排序,默认是排升序
#include<algorithm>
int main()
{
int a[] = { 1,7,5,3,2,8 };
int sz = sizeof(a) / sizeof(a[0]);
list<int> lt(a, a + sz);
sort(a, a + sz);//直接对数组排序
//sort(lt.begin(), lt.end());//err
lt.sort();//可以使用,但是不建议使用,效率低
for (auto x : lt)
{
cout << x << " ";//1 2 3 5 7 8
}
return 0;
}
如果想要排降序 -> 传仿函数 需要引用 functional
函数,此时sort的第三个参数为:greater<int>()
#include<functional>
int main()
{
int a[] = { 1,5,6,7,8 };
int sz = sizeof(a) / sizeof(a[0]);
sort(a, a + sz,greater<int>());//排降序
for (int i = 0; i < sz; i++)
{
cout << a[i] << " ";// 8 7 6 5 1
}
}
自定义排序方法
例如:
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
struct Stu
{
Stu(int _age = 0,const char* _name = "Mango")
{
age = _age;
name = _name;
}
int age;
string name;
};
bool cmp(Stu x, Stu y)
{
return x.age < y.age; //升序
}
int main()
{
Stu s[3] = { {20,"Mango"},{19,"Lemon"},{21,"Man"} };
//如果想用sort按年龄排序结构体,就需要自定义排序函数
//这里的第三个参数传入的是函数指针,不需要()进行调用
sort(s, s + 3,cmp);
for (int i = 0; i < 3; i++)
{
cout << " age: " << s[i].age << " name " << s[i].name << endl;
}
}
关于迭代器分类
单向迭代器:支持++ 如:forword_list 单链表
双向迭代器: 支持++ – 如:list 双向循环链表
随机迭代器:支持++/–/+/- 如:vector string