c++ STL--container (Part 2)

C++ STL – Containers (Part 2)

1.vector vector (serial container)

1. Features:

​ Data storage and access are more convenient. You can use [index] to access or modify values ​​like the one in the array. It is suitable for modifying and viewing a lot of elements. For operations with many inserts or erases, it will affect efficiency and is not recommended. use vector

2. The header file used is

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

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

We use the template T to instantiate a vector of type int for functional testing

1. Create a vector

vector<int> vec;//空的向量
vector<int> vec2(2);//构建了容量和使用量,并使用默认的初始化值,这里容量和使用量都是2
vector<int> vec3(2,3);//构建了容量和使用量,并指定了初始化值,这里容量和使用量都是2,初始值为3
vector<int> vec4{ 1,2,3,4 };//使用初始化列表,进行构建 这里容量和使用量都是4

int arr[5] = { 5,6,7,8,9 };//通过连续的空间(比如数组)进行构建向量
vector<int> vec5(arr, arr + 3);//左闭右开

2. Capacity, usage

cout << vec2.capacity() << "   " << vec2.size() << endl;//输出容量 使用量

3. traverse

1. Iterator traversal
vector<int>::iterator ite = vec4.begin();
	while (ite != vec4.end()) {
		cout << *ite << "   ";
		ite++;
	}
	cout << endl;
2. Range traversal
for (int v : vec5) cout << v << "   ";

4. Vector tail addition and tail deletion

Note: When the capacity is not enough, the capacity is expanded by 1.5 times
vec5.push_back(7);//尾添加
vec5.pop_back();//尾删除

5. Vector insertion and deletion

Note: When the capacity is not enough, the capacity is expanded by 1.5 times
ite = vec5.begin();
vec5.insert(ite, 4);//在指向位置之前添加

ite += 2;//此迭代器支持+=
ite=vec5.erase(ite);//删除时,迭代器会失效,可以接一下返回值,返回的是删除的下一个的迭代器

5. Empty the vector

vec.clear();// //使用量为0,容量不变,相当于是把矿泉水倒没了,瓶子还在

6. Reset a usage amount

vec.resize(3);//重新设定使用量,如果新设定的使用量小于容量,那么容量不会受影响,如果超过了容量,会导致扩容

7. Swap two vectors

vec.swap(vec2);

//可以用此方法来清空向量(容量和使用量均为0)
vector<int>().swap(vec);//这里是把已存在的向量和创建的临时变量(使用量和容量均为0)进行交换

8. Reduce the capacity to the same size as the usage

vec3.shrink_to_fit();

2. The comparison between list and vector

1.vector is a continuous space, sequential storage, list chain structure, chain storage

2. Vector is inserted at the non-tail, and deleting a node will cause the copy movement of other elements, while list will not affect other node elements

3. Vector allocates memory at one time, and when the usage is not enough, apply for memory reallocation. list will apply for memory every time a new node is inserted

4.vector has good random access performance but poor insertion and deletion performance; list has poor random access performance and good insertion and deletion performance

5.vector has the concept of capacity and usage, while list only has the concept of usage (ie length)

3.deque double-ended queue (serial container)

1. Features:

​ deque is a continuous space with two-way openings, which can insert and delete elements at both ends of the head and tail respectively-

2. The header file used is

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

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

We use the template T to instantiate a deque of type int for functional testing

1. Create a deque

deque<int> de{ 1,2,3,4 };//使用初始化列表

2. Add head, add tail, get length

de.push_front(0);//头添加
de.push_back(5);//尾添加
cout << de.size() << endl;//输出长度

3. traverse

1. Iterators
deque<int>::iterator ite = de.begin();
while (ite != de.end()) {
	cout << *ite << "   ";
	ite+=1;	
}
2. Range traversal
for (int v : de) {
	cout << v << "   ";
}

4. Access elements by subscript

for (int i = 0; i < de.size(); i += 1) {
		cout << de[i] << "   ";//通过下标访问元素
}

4.map mapping table (association container)

1. Features

All elements will be automatically sorted according to the key value of the element, and all elements of the map are pairs, which have both key values ​​and real values. Two elements are not allowed to have the same key value. The key value of the map is related to the arrangement rules of the map elements. Any change of the key value of the map element will seriously damage the organization of the map, so it is not possible to change the key of the map through the iterator of the map value, but the actual value of the element can be changed through an iterator

Search efficiency: O(log2(n)), internal implementation of red-black tree

2. The header file used is

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

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

We use the template T to instantiate a map of type (char, int) for functional testing

1. Create a map

map<char, int> m1{ {'r',1},{'q',2}};//使用初始化列表
m1['a'] = 3;//[]里是键值,=右边的是实值

2. traverse

1. Iterators
map<char, int>::iterator ite = m1.begin();
while (ite != m1.end()) {
	cout << ite->first << "-" << ite->second << endl;
	ite++;	
}
2. Range traversal
for (pair<char, int> v : m1) {
	cout << v.first << "-" << v.second<<"   ";
}

3. Insertion of elements

m1.insert(pair<char, int>('b', 4));
//如果键值存在,则插入失败,可通过bool判断,返回的迭代器指向的是原有键值的元素

//此函数的返回值为pair<迭代器,bool>,我们接一下,看一下它的bool返回值
pair<map<char, int>::iterator, bool> pr = m1.insert(pair<char, int>('b', 5));
cout << "bool =" << pr.second << endl;//bool = 0
cout << pr.first->first << "-" << pr.first->second << endl;//返回的迭代器指向的是原有键值的元素

4. Deletion of elements

ite = m1.begin();
ite = m1.erase(ite);//可以用迭代器接一下返回值,防止迭代器失效

5. Element search

ite = m1.find('r');//通过键值找,没找到返回的是无效的那个元素( m1.end() )

6. upper_bound (returns an iterator of a map greater than a certain key value), lower_bound (returns an iterator of a map greater than or equal to a certain key value)

ite = m1.upper_bound('c');//返回大于该键值的map的迭代器
ite = m1.lower_bound('c');//返回大于等于该键值的map的迭代器

//可以根据这种方法判断键值是否存在
char c = 'i';
if (m1.upper_bound(c) != m1.lower_bound(c)) {
   	cout << "键值存在" << endl;
}
else {
  	cout << "键值不存在" << endl;
}

5.set collection (association container)

1. Features

All elements will be automatically sorted according to the key value of the element. Unlike map, the elements of set can have both key value and real value at the same time. The key value of set element is the real value, and the real value is the key value. Two elements are not allowed to have the same Key value, because the element value of the set is its key value, which is related to the arrangement rules of the set elements. Any change of the set element value will seriously damage the organization of the set

Search efficiency O(log2(n)), internal implementation of red-black tree

2. The header file used is

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

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

We use the template T to instantiate a set of type int for functional testing

1. Create a set

set<int> st{4,2,6,0};//使用初始化列表

2. traverse

1. Iterators
set<int> ::iterator ite = st.begin();
while (ite != st.end()) {
	cout << *ite<< endl;
	ite++;	
}
2. Range traversal
for (int v : m1) {
	cout << v <<"   ";
}

3.set insertion

st.insert(4);
//如果键值存在,则插入失败,可通过bool判断,返回的迭代器指向的是原有键值的元素

4.set deletion

ite = st.begin();
ite = st.erase(ite);//可以用迭代器接一下返回值,防止迭代器失效

5.hash_map hash table (association container)

1. Features

Based on hashTable (hash table), data storage and search efficiency is very high, almost can be regarded as constant time, the corresponding cost is to consume more memory, use a larger array to store space, after calculation, make each There is a unique correspondence between the element and the subscript of the array, and it can be directly located when searching

Lookup efficiency O(1)

out of order

2. The header file used is

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

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

We use the template T to instantiate a hash_map of type (char, int) for functional testing

1. Create hash_map

unorder_map<char, int> m1{ {'r',1},{'q',2}};//使用初始化列表
m1['a'] = 3;//[]里是键值,=右边的是实值

2. traverse

1. Iterators
unorder_map<char, int>::iterator ite = m1.begin();
while (ite != m1.end()) {
	cout << ite->first << "-" << ite->second << endl;
	ite++;	
}
2. Range traversal
for (pair<char, int> v : m1) {
	cout << v.first << "-" << v.second<<"   ";
}

3. Insertion of elements

m1.insert(pair<char, int>('b', 4));
//如果键值存在,则插入失败,可通过bool判断,返回的迭代器指向的是原有键值的元素

4. Deletion of elements

ite = m1.begin();
ite = m1.erase(ite);//可以用迭代器接一下返回值,防止迭代器失效

5. Element search

ite = m1.find('r');//通过键值找,没找到返回的是无效的那个元素( m1.end() )

Guess you like

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