目录
一、List简介
list不能使用原生态指针,即Node*来遍历链表因为list没有提供获取头指针的方法。
- list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
- list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
- list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
- 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
- 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素。
二、迭代器失效
迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。
迭代器失效解决方法:在所有可能导致迭代器失效的操作之后,在使用迭代器之前给迭代器重新赋值。
三、List相关函数介绍
以下测试了关于List的7个函数,具体结果见注释。包含了List常用方法。
#include <list>
#include <vector>
void TestList1()
{
list<int> L1;
list<int> L2(10, 2);
for (auto e : L2)
cout << e << " "; //输出10个2
cout << endl;
vector<int> v{1,2,3};
list<int> L3(v.begin(), v.end());
for (auto e : L3)
cout << e << " "; //输出1 2 3
cout << endl;
list<int> L4(L3);
for (auto e : L4)
cout << e << " "; //输出1 2 3
cout << endl;
// C++11
list<int> L5{ 1, 2, 3, 4, 5 };
for (auto e : L5)
cout << e << " "; //输出1 2 3 4 5
cout << endl;
/
// 链表遍历
for (auto e : L2)
cout << e << " "; //输出10个2
cout << endl;
// list<int>::iterator it = L3.begin();
auto it = L3.begin();
while (it != L3.end())
{
cout << *it << " "; //输出1 2 3
++it;
}
cout << endl;
auto rit = L4.rbegin();
while (rit != L4.rend())
{
cout << *rit << " "; //输出3 2 1 注意是反向输出
++rit;
}
cout << endl;
// 注意:list不能使用原生态指针,即Node*来遍历链表
// 因为list没有提供获取头指针的方法
}
void TestList2()
{
list<int> L{ 1, 2, 3, 4, 5 };
cout << L.size() << endl; //5
L.resize(10, 6); //扩展成10个元素 用6补充没有值的部分
for (auto e : L)
cout << e << " "; //输出1 2 3 4 5 6 6 6 6 6
cout << endl;
L.resize(20);
for (auto e : L)
cout << e << " "; //输出1 2 3 4 5 6 6 6 6 6 0 0 0 0 0 0 0 0 0 0
cout << endl;
L.resize(7);
for (auto e : L)
cout << e << " "; //输出 1 2 3 4 5 6 6
cout << endl;
}
void TestList3()
{
list<int> L{ 1, 2, 3, 4, 5 };
L.push_back(6);
L.push_back(7);
L.push_back(8);
L.push_back(9);
cout << L.size() << endl; //9
cout << L.front() << endl;// 输出第一个元素 1
cout << L.back() << endl; //输出最后一个元素 9
L.pop_back(); //删除末尾元素
L.pop_back();
for (auto e : L)
cout << e << " "; //输出1 2 3 4 5 6 7
cout << endl;
L.push_front(0);
for (auto e : L)
cout << e << " "; //0 1 2 3 4 5 6 7
cout << endl;
L.pop_front();
for (auto e : L)
cout << e << " "; // 1 2 3 4 5 6 7
cout << endl;
L.insert(L.begin(), 0); //此时L为:0 1 2 3 4 5 6 7
for (auto e : L)
cout << e << " "; //0 1 2 3 4 5 6 7
cout << endl;
auto pos = find(L.begin(), L.end(), 5);
if (pos != L.end())
L.insert(pos, 5, 8); //此时L为:0 1 2 3 4 8 8 8 8 8 5 6 7
for (auto e : L)
cout << e << " "; // 0 1 2 3 4 8 8 8 8 8 5 6 7
cout << endl;
int array[] = { 10, 20, 30, 40, 50 };
L.insert(L.end(), array, array + sizeof(array) / sizeof(array[0]));//此时L为:0 1 2 3 4 8 8 8 8 8 5 6 7 10 20 30 40 50
for (auto e : L)
cout << e << " "; // 0 1 2 3 4 8 8 8 8 8 5 6 7 10 20 30 40 50
cout << endl;
}
void TestList4()
{
list<int> L{ 1, 2, 3, 4, 5 };
L.push_back(6);
L.push_back(7);
L.push_back(8);
L.push_back(9);
for (auto e : L)
cout << e << " "; // 1 2 3 4 5 6 7 8 9
cout << endl;
L.erase(L.begin());
for (auto e : L)
cout << e << " "; // 2 3 4 5 6 7 8 9
cout << endl;
L.erase(find(L.begin(), L.end(), 8));
for (auto e : L)
cout << e << " "; // 2 3 4 5 6 7 9
cout << endl;
L.erase(L.begin(), L.end()); // clear();
for (auto e : L)
cout << e << " "; //什么都不输出
cout << endl;
L.assign(10, 5);
for (auto e : L)
cout << e << " "; //5 5 5 5 5 5 5 5 5 5
cout << endl;
int array[] = { 10, 20, 30, 40, 50 };
L.assign(array, array + sizeof(array) / sizeof(array[0]));
for (auto e : L)
cout << e << " "; //10 20 30 40 50
cout << endl;
}
void TestList5()
{
list<int> L{ 2, 2, 66, 4, 5};
//L.remove(66);
for (auto e : L)
cout << e << " "; //2 2 66 4 5
cout << endl;
L.sort();
for (auto e : L)
cout << e << " "; //2 2 4 5 66
cout << endl;
L.unique();
for (auto e : L)
cout << e << " "; //2 4 5 66
cout << endl;
L.reverse();
for (auto e : L)
cout << e << " "; //66 5 4 2
cout << endl;
}
bool IsEven(int data)
{
// 5的二进制:0000 0101
// 0000 0001
// return 0 == (data & 0x01);
return 0 == data % 2;
}
void TestList6()
{
list<int> L{ 1, 2, 3, 4, 5 };
L.push_back(6);
L.push_back(7);
L.push_back(8);
L.push_back(9);
for (auto e : L)
cout << e << " "; //1 2 3 4 5 6 7 8 9
cout << endl;
L.remove_if(IsEven); //移除了偶数 IsEven返回值为真表示该值为偶数
for (auto e : L)
cout << e << " "; //1 3 5 7 9
cout << endl;
}
void TestList7()
{
list<int> L{ 1, 2, 3, 4, 5 };
L.push_back(6);
L.push_back(7);
L.push_back(8);
L.push_back(9);
for (auto e : L)
cout << e << " "; //1 2 3 4 5 6 7 8 9
cout << endl;
auto it = L.begin();
//pop_front导致的迭代器失效
/*L.pop_front();
while (it != L.end())
{
cout << *it << " "; //程序崩溃
++it;
}*/
//resize导致的迭代器失效
/*L.resize(0);
while (it != L.end())
{
cout << *it << " "; //程序崩溃
++it;
}*/
assign导致的迭代器失效
//L.assign(10, 5);
// while (it != L.end())
//{
// cout << *it << " "; //老师演示的情况时程序崩溃 但测试还是会输出10个5
// ++it;
//}
//cout << endl;
// L.assign(10, 5);
//list<int> L1{ 10, 20, 30, 40, 50 };
//L = L1;
//while (it != L.end())
//{
// cout << *it << " "; //输出:10 20 30 40 50
// ++it;
//}
//cout << "第一次打印" << endl;
//cout << endl;
//erase导致的迭代器失效
//while (it != L.end())
//{
// L.erase(it); //程序崩溃
// ++it;
// //it = L.erase(it);
//}
while (it != L.end())
{
//L.erase(it);
//++it;
it = L.erase(it); //改用该语句程序不会崩溃 删除了list中的每个元素
}
for (auto e : L)
cout << e << " "; //输出空
cout <<"已删除list中每一个元素"<< endl;
cout << 1111 << endl;
}
int main()
{
//TestList1();
//TestList2();
//TestList3();
//TestList4();
//TestList5();
//TestList6();
TestList7();
return 0;
}
四、List和Vector的对比
vector | list | |
底 层 结 构 | 动态顺序表,一段连续空间 | 带头结点的双向循环链表 |
随 机 访 问 | 支持随机访问,访问某个元素效率O(1) | 不支持随机访问,访问某个元素 效率O(N) |
插 入 和 删 除 | 任意位置插入和删除效率低,需要搬移元素,时间复杂 度为O(N),插入时有可能需要增容,增容:开辟新空 间,拷贝元素,释放旧空间,导致效率更低 | 任意位置插入和删除效率高,不 需要搬移元素,时间复杂度为 O(1) |
空 间 利 用 率 | 底层为连续空间,不容易造成内存碎片,空间利用率 高,缓存利用率高 | 底层节点动态开辟,小节点容易 造成内存碎片,空间利用率低, 缓存利用率低 |
迭 代 器 | 原生态指针 | 对原生态指针(节点指针)进行封装 |
迭 代 器 失 效 | 在插入元素时,要给所有的迭代器重新赋值,因为插入 元素有可能会导致重新扩容,致使原来迭代器失效,删 除时,当前迭代器需要重新赋值否则会失效 | 插入元素不会导致迭代器失效, 删除元素时,只会导致当前迭代 器失效,其他迭代器不受影响 |
使 用 场 景 | 需要高效存储,支持随机访问,不关心插入删除效率 | 大量插入和删除操作,不关心随 机访问 |