STL库list
发表于:2024-12-21 | 分类: C++
字数统计: 735 | 阅读时长: 3分钟 | 阅读量:

list

构造

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>
#include <list> //头文件

using namespace std;

int main()
{
list<int> lst1; // 创建空list
list<int> list1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // 初始化列表
list<int> lst2(3); // 创建含n个默认值的list
list<int> lst3(3, 3); // 创建含n个elem拷贝的list
list<int> lst4(lst2); // 拷贝构造函数(和直接赋值为list一样)

// 使用迭代器区间构造
vector<int> vec = {1, 2, 3, 4, 5};
list<int> lst5(vec.begin(), vec.end());
return 0;
}

api

基本api

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <list>

using namespace std;

int main()
{
list<int> list1(10, 10);

// 尾部插入元素
list1.push_back(1);
list1.emplace_back(20);

// 头部插入元素
list1.push_front(-1);

// 在pos位置插入elem,参数1是迭代器
list1.insert(list1.begin(), 3);

// 删除首元素
list1.pop_front();

// 删除尾元素
list1.pop_back();

// 删除pos位置元素
list1.erase(list1.begin());

// 删除所有值为elem的元素
list1.remove(0);

// 返回首元素
cout << list1.front() << endl;

// 返回尾元素
cout << list1.back() << endl;

// 判断list是否为空
cout << list1.empty() << endl; // 空为1

// 返回list的元素个数
cout << list1.size() << endl;

// 清空list
list1.clear();
return 0;
}

其他api

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <list>

using namespace std;

int main()
{
list<int> list1 = {1, 2, 2, 3, 4, 5, 6, 7, 9, 8, 10};

// 反转链表
list1.reverse();

// 删除重复连续元素(有顺序去重)
list1.unique();

// 排序
list1.sort();

// 插入list
list<int> list2 = {999, 888};
list1.splice(list1.begin(), list2); // 插入到list1的begin位置并清空list2

// 合并两个链表
list<int> list3 = {1, 3,2};
list<int> list4 = {3, 4, 1};
list3.merge(list4); // 合并到list3尾部,合并后list4为空
return 0;
}

访问和遍历

注意:

  • vector的it支持++,–,>=,+,-等运算

  • list的it仅支持++和–

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <list>

using namespace std;

int main()
{
list<int> list1 = {2, 2, 3, 4, 4, 5};

// 底层链表,不支持下标。通过迭代器访问
auto it = list1.begin();
// 迭代器移动到第3个元素
advance(it, 2);
cout << *it << endl;

// 遍历(参考vector)

// 普通for循环
auto it1 = list1.begin();
for (int i = 0; i < list1.size(); i++)
{
cout << *it1 << " ";
it1++;
}
cout << endl;

// 迭代器for
for (auto it2 = list1.begin(); it2 != list1.end(); it2++)
{
cout << *it2 << " ";
}
cout << endl;

// c++ 11
for (auto &item : list1)
{
cout << item << " ";
}
return 0;
}

并发修改

参考vector,直接给正确方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <list>

using namespace std;

int main()
{
list<int> list1 = {2, 2, 2, 2};

// 更新迭代器指向
for (auto it = list1.begin(); it != list1.end();)
{
if (*it == 2)
{
it = list1.erase(it);
}
else
{
++it;
}
}

list<int> list2 = {2, 2, 2, 2};
// remove函数
list2.remove(2);

// remove_if函数
list<int> list3 = {2, 2, 2, 2};
list3.remove_if([](int i)
{ return i == 2; });
return 0;
}

底层

双向链表

上一篇:
STL库map
下一篇:
STL库vector