STL容器之list的介绍与使用
1.list的介绍
- list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
- list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
- list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效
- 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好(O(1)的时间复杂度)。
- 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问(O(n)的时间复杂度),比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)
- 与vector不同的是, list中让迭代器失效的只有erase函数, 插入操作(insert)和合并操作(splice)都不会使迭代器失效。
2.list的使用
2.1list的定义
//头文件
#include<list>
list<datatype> name;
2.2list的常见构造
构造函数 | 接口说明 |
---|---|
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 |
#include<iostream>
#include<list>
using namespace std;
void Print_List(list<int>& l) {
for (list<int>::iterator it = l.begin(); it != l.end(); ++it) {
cout << *it << " ";
}
cout << endl;
}
int main() {
list<int> L1;
list<int> L2(4, 12);
list<int> L3(L2.begin(), L2.end());
list<int> L4(L2);
//以数组为迭代器构造list
int array[] = { 2,3,4,5,6 };
list<int> L5(array, array + sizeof(array) / sizeof(int));
cout << "L1:";
Print_List(L1);
cout << "L2:";
Print_List(L2);
cout << "L3:";
Print_List(L3);
cout << "L4:";
Print_List(L4);
cout << "L5:";
Print_List(L5);
return 0;
}
2.3list iterator的使用
在list中我们可以将迭代器理解成一个指针,该指针指向list中的某个节点。
函数声明 | 接口说明 |
---|---|
begin() | 返回第一个元素的iterator |
cbegin() (C++11) | 返回第一个元素的cosnt_iterator |
end() | 返回最后一个元素下一个位置的iterator |
cend() (C++11) | 返回最后一个元素下一个位置的const_iterator |
rbegin() | 返回最后一个元素的位置的reverse_iterator |
crbegin() (C++11) | 返回最后一个元素的位置的const_reverse_iterator |
rend() | 返回第一个元素前一个位置的reverse_iterator |
crend() (C++11) | 返回第一个元素前一个位置的const_reverse_iterator |
- begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动。
- rbegin与rend为反向迭代器,对迭代器执行++操作,迭代器向前移动。
- cbegin与cend为const的正向迭代器,与begin和end不同的是:该迭代器指向节点中的元素值不能修改。
- crbegin与crend为const的反向迭带器,与rbegin和rend不同的是:该迭代器指向节点中的元素值不能修改。
#include<iostream>
#include<list>
using namespace std;
int main() {
int array[] = { 12,13,14,15,16,17 };
list<int> La(array, array + sizeof(array) / sizeof(int));
//使用正向迭代器打印list
cout << "使用正向迭代器打印list" << endl;
cout << "La:";
for (list<int>::iterator it = La.begin(); it != La.end(); ++it) {
cout << *it << " ";
}
cout << endl;
//使用反向迭代器打印list
cout << "使用反向迭代器打印list" << endl;
cout << "La:";
for (list<int>::reverse_iterator rit = La.rbegin(); rit != La.rend(); ++rit) {
//反向迭代器,对迭代器执行++操作,迭代器向前移动
cout << *rit << " ";
}
cout << endl;
return 0;
}
2.4list的容量操作
函数名称 | 接口说明 |
---|---|
size_t size() const | 返回list中的元素个数 |
bool empty() const | 检测list是否为空,是返回true,否则返回false |
void clear() | 清空list |
#include<iostream>
#include<list>
using namespace std;
int main() {
int array[] = { 12,13,14,15,16,17 };
list<int> La(array, array + sizeof(array) / sizeof(int));
cout << "La的元素个数:" << La.size() << endl;
if (La.empty())
cout << "La为空!" << endl;
else
{
for (auto i : La) {
cout << i << " ";
}
cout << endl;
}
return 0;
}
2.5list中的元素访问
函数名称 | 接口说明 |
---|---|
reference front() | 返回list的第一个节点中值的引用 |
const_reference front() const | 返回list的第一个节点中值的const引用 |
reference back() | 返回list的最后一个节点中值的引用 |
const_reference back() const | 返回list的最后一个节点中值的const引用 |
#include<iostream>
#include<list>
using namespace std;
int main() {
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
list<int> l1(array, array + sizeof(array) / sizeof(array[0]));
for (auto& e : l1)
cout << e << " ";
cout << endl;
// 将list中第一个节点与最后一个节点中的值改为10
l1.front() = 10;
l1.back() = 10;
for (auto& e : l1)
cout << e << " ";
cout << endl;
const list<int> l2(array, array + sizeof(array) / sizeof(array[0]));
//因为l2是const类型的list对象,因此其front()为const引用类型
const int& ca = l2.front();
return 0;
}
2.5list中的修改操作
函数声明 | 重载函数 | 接口说明 |
---|---|---|
void push_front (const value_type& val) | 无 | 在list首元素前插入值为val的元素(头插) |
void pop_front() | 无 | 删除list中第一个元素(头删) |
void push_back (const value_type& val) | 无 | 在list尾部插入值为val的元素 (尾插) |
void pop_back() | 无 | 删除list中最后一个元素(尾删) |
insert | iterator insert (iterator position, const value_type& val) void insert (iterator position, size_type n, const value_type&val) void insert (iterator position, InputIterator first, InputIterator last) | 在list position 位置中插入值为val的元素 在list position位置插入n个值为val的元素 在list position位置插入[first, last)区间中元素 |
erase | iterator erase (iterator position) iterator erase (iterator first, iterator last) | 删除list position位置的元素 删除list中[first, last)区间中的元素 |
void swap (list& x) | 无 | 交换两个list中的元素 |
void resize (size_type n, value_type val = value_type()) | 无 | 将list中有效元素个数改变到n个,多出的元素用val填充 |
void clear() | 无 | 清空list中的有效元素 |
3.list的迭代器失效
前面说过,大家可将迭代器暂时理解成类似于指针,迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。
#include<iostream>
#include<list>
using namespace std;
int main() {
int array[] = { 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0 };
list<int>l(array, array + sizeof(array) / sizeof(array[0]));
auto it = l.begin();
while (it != l.end())
{
//l.erase(it);
//++it;
// erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给其赋值
it = l.erase(it);
}
return 0;
}
4.list与vector对比
内容 | vector | list |
---|---|---|
底层结构 | 动态顺序表,一段连续空间 | 带头结点的双向循环链表 |
随机访问 | 支持随机访问,访问某个元素效率O(1) | 不支持随机访问,访问某个元素效率O(N) |
插入和删除 | 任意位置插入和删除效率低,需要搬移元素,时间复杂度为O(N),插入时有可能需要增容,增容:开辟新空间,拷贝元素,释放旧空间,导致效率更低 | 任意位置插入和删除效率高,不需要搬移元素,时间复杂度为O(1) |
空间利用率 | 底层为连续空间,不容易造成内存碎片,空间利用率高,缓存利用率高 | 底层节点动态开辟,小节点容易造成内存碎片,空间利用率低,缓存利用率低 |
迭代器 | 原生态指针 | 对原生态指针(节点指针)进行封装 |
迭代器失效 | 在插入元素时,要给所有的迭代器重新赋值,因为插入元素有可能会导致重新扩容,致使原来迭代器失效,删除时,当前迭代器需要重新赋值否则会失效 | 插入元素不会导致迭代器失效,删除元素时,只会导致当前迭代器失效,其他迭代器不受影响 |
使用场景 | 需要高效存储,支持随机访问,不关心插入删除效率 | 大量插入和删除操作,不关心随机访问 |