c++ STL--container (Part 1)

C++ STL – Containers (Part 1)

1. STL

1. Some knowledge about STL

1. The full name of STL: Standard Template Library

2. STL Chinese name is Standard Template Library

3. STL is part of the C++ standard library and is provided in the form of source code

4. STL embodies the idea of ​​​​generic programming (can operate on different data types)

5. The header files of STL do not add extensions, and the std namespace needs to be opened

2. Six components of STL

1. Container

2. Iterators

3. Algorithms

4. Container Adapter

5. Space allocator

6. Functors

2. Container

1. Containers are divided into two categories

1. Sequential container:

​ The element maintains its original position in the container, allowing insertion and deletion at specified positions. Each element has a fixed position, which depends on the time and place of insertion, regardless of the value of the element. For example, a list container

2. Associative container:

​ The position of the element depends on the specific sorting rules of the container, generally related to the value of the element such as map container

2. list container (serial container)

1. Features

The efficiency of adding and deleting elements in any position of the list remains the same. The time complexity is O(1)

But lookup efficiency is O(n)

2. The header file used is

#include<list>
using namespace std;//需要打开std命名空间

3. About the use of some functions of the list container

We use the template T to instantiate a linked list of type int for functional testing
1. Create a linked list
list<int> //创建链表
list<int> lst(5);//创建链表 并指定链表长度
list<int> lst(5,4);//指定链表长度,且指定了初始化
list<int>  lst{ 1,2,3,4,5 };//使用初始化列表 进行显示的指定
2. Remove the node corresponding to the value in the linked list
list<int> lst.remove(2);//将链表中值为2的节点移除
3. Remove consecutive and identical nodes and only one remains
lst.unique();
4. Traversal of the linked list
1. Iterator traversal
list<int>::iterator ite = lst.begin();	
while (ite != lst.end()) {//lst.end()是一个无效节点,是有效节点的下一个
	cout << *ite << "   ";
	ite++;
}
2. Range traversal
for (int v : lst) cout << v << "   ";
5. Sorting and sorting of linked lists
1. Ascending order
//第一种
list<int> lst2{ 2,8,6,4,7,9,1 };
lst2.sort();//默认升序
for (int v : lst2) cout << v << "   ";//遍历一下

//第二种
bool rule_asc(int a, int b) {
	return a < b;
}

lst2.sort(&rule_asc);//指定规则:升序
	for (int v : lst2) cout << v << "   ";//遍历一下

//第三种
lst2.sort(less<>());//升序,用写好的升序规则
	for (int v : lst2) cout << v << "   ";//遍历一下
2. descending order
//第一种
bool rule_desc(int a,int b) {
	return a > b;
}

lst2.sort(&rule_desc);//指定规则:降序
	for (int v : lst2) cout << v << "   ";//遍历一下

//第二种
lst2.sort(greater<>());//降序,用写好的降序规则
	for (int v : lst2) cout << v << "   ";//遍历一下
6. Flipping the linked list
lst2.reverse();
7. Cutting of the linked list
list<int> lst3{ 5,2,0 };

list<int>::iterator ite = lst2.begin();//迭代器找位置
::advance(ite, 3);//迭代器做偏移,支持正向,反向偏移

//第一种方式
lst2.splice(ite, lst3);//剪切,第一个参数是位置(被剪切的链表要插入到这个位置之前),第二个参数是被剪切的链表
for (int v : lst2) cout << v << "   ";//遍历一下

//第二种方式
lst2.splice(ite, lst3, lst3.begin());//剪切 某一个节点
for (int v : lst2) cout << v << "   ";//遍历一下

//第三种方式
lst2.splice(ite, lst3, lst3.begin(),--lst3.end());//剪切 某一段链表中的节点
for (int v : lst2) cout << v << "   ";//遍历一下
8. Merge of linked lists (two linked lists must be ordered and in the same order to be merged)
//合并后升序
lst2.sort();
lst3.sort();
lst2.merge(lst3);//合并,合并完是升序
for (int v : lst2) cout << v << "   ";//遍历一下

//合并后降序
lst2.sort(greater<>());
lst3.sort(greater<>());
lst2.merge(lst3,greater<>());//合并,合并完是升序,所以进行一个降序规则的指定
for (int v : lst2) cout << v << "   ";//遍历一下
9. Swap two linked lists
lst2.swap(lst3);//交换
for (int v : lst2) cout << v << "   ";//遍历一下
for (int v : lst3) cout << v << "   ";//遍历一下

Guess you like

Origin blog.csdn.net/m0_73483024/article/details/132237177