C++ - STL (on)

foreword

Basic concepts of STL

1. STL (Standard Template Library, standard template library)

2. STL is broadly divided into: container (container), algorithm (algorithm), iterator (iterator)

3. Seamless connection between containers and algorithms through iterators

4. Almost all codes in STL use template classes or template functions


1. Getting to know STL for the first time

1.1 STL is roughly divided into six major components

They are: container, algorithm, iterator, functor, adapter (adapter), space configurator

1. Container: Various data structures, such as vector, list, deque, set, map, etc., are used to store data

2. Algorithms: various commonly used algorithms, such as sort, find, copy, for_each, etc.

3. Generation selector: acts as the glue between the container and the algorithm

4. Functor: Behavior is similar to a function, which can be used as a certain strategy of the algorithm

5. Adapter:  A thing used to modify a container or functor or iterator interface

6. Space configurator:  responsible for space configuration and management

1.1.1 Containers

Container: a place for storage

STL containers are commonly used data structures that implement some of the most widely used data structures : arrays, linked lists, trees, stacks, queue sets, mapping tables, etc. These containers are divided into two types : sequential containers and associative containers :

Sequential container:  Emphasizes the sorting of values, and each element in the sequential container has a fixed position.

Associative container:  a binary tree structure, there is no strict physical order relationship between elements.

1.1.2 Algorithms

Algorithms: Solutions to Problems

Limited steps to solve logical or mathematical problems, this subject we call Algorithms (Algorithms) Algorithms are divided into: qualitative change algorithm and non-qualitative change algorithm .

Qualitative change algorithm: It means that the content of the elements in the interval will be changed  during the operation . such as copy, replace, delete, etc.

Non-qualitative algorithm:   means that the content of the elements in the interval will not be changed during the operation , such as searching, counting, traversing, finding extreme values, etc.

 1.1.3 Iterators

Iterators: the glue between containers and algorithms

Provides a way to sequentially access the elements of a container without exposing the container's internal representation Each container has its own iterator

The use of iterators is very similar to pointers . At the beginning stage, we can first understand that iterators are pointers.

*** Commonly used are bidirectional iterators and random access iterators

2. Containers, Algorithms, Iterators

The most commonly used container in STL is vector, which can be understood as an array

2.1 vector containers

Container: vector

Algorithm: for_each

Iterator: vector<int>::iterator

In the following example, the vector container needs to include the header file   " #include <vector>when used , and the "push_back()" function is used to insert data. begin()   points to the first element in the container for the starting iterator . end() ends the iterator pointing to the next position of the last element in the container.

The steps to create a vector container can be divided into three steps :

1. Create a container

2. Add data to the container

3. Traverse the data in the container

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>//标准算法头文件
//vector容器存放内置数据类型
void myprint(int val)
{
	cout << val << endl;
}
void  test01()
{
	//1.创建了一个vector容器(可以理解为数组)
	vector<int> v;//vector容器在使用时需要包含头文件
	//2.向容器中插入数据
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	//3.通过迭代器访问容器中的数据
	vector<int>::iterator itbegin = v.begin();//begin()起始迭代器 指向容器中第一个元素
	vector<int>::iterator itend = v.end();//end()结束迭代器 指向容器中最后一个元素的下一个位置
	//第一种遍历方式
	while (itbegin != itend)
	{
		cout << *itbegin << endl;
		itbegin++;
	}
	//第二种遍历方式
	cout << "第二种遍历方式" << endl;
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << endl;
	}
	//第三种遍历方式 利用STL提供的遍历算法
	cout << "第三种遍历方式" << endl;
	for_each(v.begin(), v.end(), myprint);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

 The first traversal method

Use while loop to traverse

 The second traversal method

//The second traversal method

for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
    cout << *it << endl;
}

The third traversal method uses the traversal algorithm provided by STL 

Use the for_each() function provided by STL, but using this function needs to include the standard algorithm header file " #include <algorithm>  ".

void myprint(int val)
{
    cout << val << endl;
}

//The third traversal method uses the traversal algorithm provided by STL

for_each(v.begin(), v.end(), myprint);

 2.2 Put a custom array in the vector container

#include <iostream>
using namespace std;
#include <string>
#include <vector>
#include <algorithm>//标准算法头文件
//vector容器存放自定义数据类型
class person
{
public:
	person(string name,int age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	string m_name;
	int m_age;
};
void test01()
{
	//1.创建容器
	vector<person> v;
	person p1("li", 18);
	person p2("chen", 21);
	person p3("shuo", 23);
	person p4("wang", 20);
	person p5("wei", 22);
	//2.向容器中添加数据
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	//3.遍历容器中的数据
	for (vector<person>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "姓名:" << (*it).m_name << "  年龄:" << it->m_age << endl;
		//it本质为一个指针,(*it)解引用后就是vector后面<>里面的数据类型,这里就是person
		//it也可以像指针那样直接用 -> 指向属性
	}
}
//存放自定义数据类型  指针
void test02()
{
	//1.创建容器
	vector<person*> v;
	person p1("li", 18);
	person p2("chen", 21);
	person p3("shuo", 23);
	person p4("wang", 20);
	person p5("wei", 22);
	//2.向容器中添加数据
	v.push_back(&p1);
	v.push_back(&p2);
	v.push_back(&p3);
	v.push_back(&p4);
	v.push_back(&p5);
	//3.遍历容器中的数据
	for (vector<person*>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "test2姓名:" << (*it)->m_name << "  test2年龄:" << (*it)->m_age << endl;//这里的(*it)是一个指针
	}
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

The vector container in test01() is a person class. The it here is essentially a pointer. After (*it) is dereferenced, it is the data type in the <> behind the vector, here is the person. It can also be used directly like a pointer -> to point to an attribute.

The vector container in test02() is a pointer to person*. Here (*it) is of type person* after dereferencing, which is a pointer. It needs (*it)->m_name and (*it)->m_age to point to Attributes.

2.3 Container nested container

A container is nested inside a container, which is equivalent to a two-dimensional array. Two for loops are required for traversal.

#include <iostream>
using namespace std;
#include <string>
#include <vector>
//容器嵌套容器
void test01()
{
	vector<vector<int>> v;
	//创建小容器
	vector<int> v1;
	vector<int> v2;
	vector<int> v3;
	vector<int> v4;
	//向小容器里添加数据
	for (int i = 0; i < 4; i++)
	{
		v1.push_back(i + 1);
		v2.push_back(i + 2);
		v3.push_back(i + 3);
		v4.push_back(i + 4);
	}
	//将小容器插入到大容器中
	v.push_back(v1);
	v.push_back(v2);
	v.push_back(v3);
	v.push_back(v4);
	//通过大容器,把所有数据遍历一遍
	for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)
	{
		//(*it)——容器vector<int>
		for (vector<int>::iterator it2 = (*it).begin(); it2 != (*it).end(); it2++)
		{
			cout << *it2 << " ";
		}
		cout << endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}

Three, string container

3.1 Basic concept of string container

Essence: string is a C++-style string, and string is essentially a class

Features:
The string class internally encapsulates many member methods

For example: find find, copy copy, delete delete replace replace, insert insert string

Manage the memory allocated by char*, don't worry about copying out of bounds and value out of bounds, etc., the class is responsible for it

3.2 string constructor

  

3.3 string assignment operation

 

 3.4 string concatenation

 The following are examples of the above seven operations:

#include <iostream>
using namespace std;
#include <string>
#include <vector>
//字符串拼接

void test01()
{
	string str1 = "我";
	str1 += "爱学C++";//"+="可以拼接string容器内的内容
	cout << "str1 = " << str1 << endl;

	str1 += '!';//可以追加一个字符
	cout << "str1 = " << str1 << endl;

	string str2 = "还有C语言和java";
	str1 += str2;
	cout << "str1 = " << str1 << endl;

	string str3 = "I";
	str3.append(" love");//可以利用 append()拼接
	cout << "str3 = " << str3 << endl;

	str3.append(" python 111", 7);
	cout << "str3 = " << str3 << endl;

	str3.append(str2);
	cout << "str3 = " << str3 << endl;

	string str4;
	str4 = "我爱打游戏";
	str4.append(str2, 0, 9);//一个中文占两个字节,一个英文一个字节。这里截取0~9个字节为:“还有C语言”
	cout << "str4 = " << str4 << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

 The result of running the above code is:

str1 = I love to learn C++
str1 = I love to learn C++!
str1 = I love to learn C++! And C and java
str3 = I love
str3 = I love python
str3 = I love python and C and java
str4 = I love There is also C language for playing games
, please press any key to continue. . .

 3.5 string search and replace

Function description:
Find:   Find whether the specified string exists
Replace:   Replace the string at the specified position 

#include<iostream>
using namespace std;
#include<string>
//字符串的查找替换
//1.查找(find ,rfind)
void test01()
{
	string str1 = "abcdefgab";
	int pos = str1.find("ab");
	if (pos == -1)
	{
		cout << "未找到字符串" << endl;
	}
	else
	{
		cout<<"找到,pos=" << pos << endl;
	}
	//rfind
	pos=str1.rfind("ab");
	cout << "pos=" << pos << endl;
	//find和rfind的区别
	//find从左往右查找  rfind从右往左查找
}
//2.替换(replace)
void test02()
{
	string str2="abcdefg";
	str2.replace(1, 3, "111");//代表下标1~3用111代替
	cout << "str2=" << str2<<endl;

}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

The result of the operation is

found, pos=0
pos=7
str2=a111efg

Summarize:

1. find searches from left to back, rfind from right to left

2. find returns the position of the first character found after finding the string, and returns -1 if not found

3. When replacing, you need to specify from which position, how many characters, and what kind of string to replace

3.6 string string comparison

String comparison (using the compare() function) is based on the character's ASClI code

1. = return 0

2. > return 1

3. < return -1

Function prototype:

1. int compare(const string &s ) const; //Compare with string s

2. int compare( const char *s ) const;//compare with string s

The following is the case code:

#include<iostream>
using namespace std;
#include<string>
//字符串的比较
void test01()
{
	string str1 = "hellow";
	string str2 = "hellow";
	if (str1.compare(str2) == 0)
	{
		cout << "str1等于str2" << endl;
	}
	else if (str1.compare(str2) > 0)
	{
		cout << "str1大于str2" << endl;
	}
	else
	{
		cout << "str1小于str2" << endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}

3.7 string character access

There are two ways to access a single character in string:

1. char& operator[](int n); //Get characters by [] (equivalent to overloading square brackets)

2. char& at(int n); //Get characters through the at method

3.8 string insertion and deletion

Function description: Insert (insert) and delete (erase) characters on string strings

Function prototype:

1. string& insert(int pos, const char* s); //insert string

2. string& insert(int pos, const string& str); //insert string

3. string& insert(int pos, int n, char c); //Insert n characters c at the specified position 

4. string& erase(int pos, int n = npos); // delete n characters starting from pos


The following is the example code, insert(1, "***") means to insert " *** " from the character whose subscript is 1,

erase(1, 3) means to delete the characters from subscript 1~3.

#include<iostream>
using namespace std;
#include<string>
//字符串插入与删除
void test01()
{
	string str = "hellow";
	//插入
	str.insert(1, "***");
	cout << "插入后str = " <<str<< endl;
	//删除
	str.erase(1, 3);
	cout << "删除后str = " << str << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

 After running, the result is as follows

After inserting str = h***ellow
After deleting str = hellow
Please press any key to continue. . . 

 3.9 string substring

Description: Get the desired substring from a string

Function prototype:

string substr(int pos = 0, int n = npos) const; // return a string consisting of n characters starting from pos

The following is a case of reading a mailbox username

#include<iostream>
using namespace std;
#include<string>
void test01()
{
	string email = "[email protected]";
	//从邮箱地址中获取用户名信息
	int pos=email.find("@");//查找@的位置
	string usename = email.substr(0, pos);//需要从0开始截取8个
	cout << "用户名:" << usename << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

 The result of the operation is as follows:

Username: zhangsan
Please press any key to continue. . .

Four, vector container 

4.1 Basic concept of vector

Function: The vector data structure is very similar to an array, also known as a single-ended array

The difference between vector and ordinary array : The difference is that the array is a static space, while the vector can be dynamically expanded

 Dynamic expansion:  instead of adding a new space after the original space, find a larger memory space, and then copy the original data to the new space to release the original space

*** The iterator of the vector container is an iterator that supports random access

4.2 vector constructor

Function prototype:
1. vector<T> v; //Template implementation class implementation, default constructor

2. vector(v.begin(), v.end() ); //Copy the elements in the interval v[begin(), end()) to itself.

3. vector(n, elem); //The constructor copies n elems to itself.

4. vector(const vector &vec); //copy constructor.

The following is a demonstration case:

#include<iostream>
using namespace std;
#include<vector>
//vector容器构造
void printvector(vector<int>&v)//引用是传递首地址,不然拷贝传值会需要占用很大内存
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vector<int>v1;//默认构造
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printvector(v1);
	//2.通过区间方式进行构造、
	vector<int>v2(v1.begin(), v1.end());
	printvector(v2);
	//3.n个elem方式构造
	vector<int>v3(10, 100);//代表10个100
	printvector(v3);
	//4.拷贝构造
	vector<int>v4(v3);
	printvector(v4);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

4.3 vector assignment operation 

Function description: assign value to vector container

Function prototype:
1. vector& operator=(const vector &vec); //overload the equal sign operator
assign(beg, end); //assign the data copy in [beg,end) range to itself
assign(n, elem ); //Assign n copies of elem to itself.

#include<iostream>
using namespace std;
#include<vector>
//vector的赋值

void printvector(vector<int> &v)//自定义打印函数
{
	for (vector<int>::iterator it = v.begin(); it != v.end();it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printvector(v1);
	//赋值
	//方法一:用“=”号赋值
	vector<int>v2;
	v2 = v1;
	printvector(v2);
	//方法二:用assign赋值
	vector<int>v3;
	v3.assign(v1.begin(), v1.end());
	printvector(v3);
	//方法三:用n个elem赋值
	vector<int>v4;
	v4.assign(5,100);//5个100
	printvector(v4);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

4.4 vector capacity and size

Function description: operate on the capacity and size of the vector container

Function prototype:
1. empty(); //judging whether the container is empty

2. capacity(); //The capacity of the container

3. size(); //Return the number of elements in the container
resize(int num); //Respecify the length of the container as num, if the container becomes longer, fill the new position with the default value. If the container gets shorter, elements at the end that exceed the length of the container are removed.

4. resize(int num, elem); //Respecify the length of the container as num, if the container becomes longer, fill the new { with elem value . If the container gets shorter, elements at the end that exceed the length of the container are removed

The following are empty(), capacity(), size() three function demonstration codes:

#include<iostream>
using namespace std;
#include<vector>
//vector的容量和大小
void printvector(vector<int>& v)//自定义打印函数
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vector<int>v1;
	for (int i = 0; i <10; i++)
	{
		v1.push_back(i);
	}
	printvector(v1);
	if (v1.empty())
	{
		cout << "容器为空" << endl;
	}
	else
	{
		cout << "容器不为空,容量为:" <<v1.capacity() <<endl;
		cout << "容器的大小为:" << v1.size() << endl;
	}
	//重新指定大小
	v1.resize(15,-1);//利用重载版本,可以指定默认填充值(这里改成了用-1填充)
	printvector(v1);//数据不足,默认用0填充

	v1.resize(5);
	printvector(v1);//大小比原先小,超出的部分会默认删除
}
int main()
{
	test01();
	system("pause");
	return 0;
}

4.5 Insertion and deletion of vector containers

Function description: Insert and delete operations on vector containers 

Function prototype:

1. push_back(ele); // insert element ele at the end

2. pop_back(); //Delete the last element

3. insert(const_iterator pos,ele); //The iterator points to position pos to insert element ele

4. insert(const_iterator pos, int count,ele); //The iterator points to position pos to insert count elements

5. eleerase(const_iterator pos ); //Delete the element pointed to by the iterator

6. erase(const_iterator start, const_iterator end);//Delete the elements between start and end of the iterator

7. clear(); //Delete all elements in the container

The following is the demo code for the above function:

#include<iostream>
using namespace std;
#include<vector>
//vector的插入和删除
void printvector(vector<int>& v)//自定义打印函数
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vector<int>v1;
	for (int i = 10; i <= 50; i=i+10)
	{
		v1.push_back(i);
	}
	printvector(v1);
	//尾删( pop_back() )
	v1.pop_back();
	printvector(v1);
	//插入( insert() )(第一个参数是迭代器)
	v1.insert(v1.begin(), 0);//在第0个下标插入0
	printvector(v1);
	v1.insert(v1.begin() + 3, 2, 25);//在第3个下标插入2个25
	printvector(v1);
	//删除(erase())(参数也是迭代器)
	v1.erase(v1.begin());
	printvector(v1);
	v1.erase(v1.begin(),v1.end()-1);//删除最后一个数前的所有数字
	printvector(v1);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

The following is the result of the operation: 

10 20 30 40 50
10 20 30 40
0 ​​10
20 30 40 0 ​​10 20 25 25 30 40
10 20 25 25 30 40 40
Press
any key to continue. . .

summary:

Tail plug --- push_back

Tail delete --- pop_back

insert --- insert(position iterator)

delete --- erase(position iterator)

Empty --- clear

4.6 vector data access

Function description: access to the data in the vector

Function prototype:

at(int idx); //return the data pointed by the index idx

operator[ ]; //Return the data pointed to by the index idx

front(); //returns the first data element in the container

back(); //returns the last data element in the container

The following is the demo code:

#include<iostream>
using namespace std;
#include<vector>
//vector的数据存取

void test01()
{
	vector<int>v1;
	for (int i = 0; i <10; i++)
	{
		v1.push_back(i);
	}
	//利用[]方式访问数组中的元素
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << " ";
	}
	cout << endl; 
	//利用at方式访问元素
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1.at(i) << " ";
	}
	cout << endl;
	//获取第一个元素
	cout << "第一个元素为:" << v1.front() << endl;
	cout<< "最后一个元素为:" << v1.back() << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

 summary:

1. In addition to using iterators to obtain elements in the vector container, [] and at can also be used

2. front returns the first element of the container

3. back returns the last element of the container

4.7 vector swap container

Function description: realize the exchange of elements in two containers

Function prototype:  swap(vec); //interchange vec with its own elements

The following is the demo code:

#include<iostream>
using namespace std;
#include<vector>
//vector的插入和删除
void printvector(vector<int>& v)//自定义打印函数
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	cout << "交换前:" << endl;
	vector<int>v1;
	for (int i = 0; i <10; i++)
	{
		v1.push_back(i);
	}
	printvector(v1);
	vector<int>v2;
	for (int i = 10; i>0; i--)
	{
		v2.push_back(i);
	}
	printvector(v2);
	cout << "交换后:" << endl;
	v1.swap(v2);
	printvector(v1);
	printvector(v2);
}
//2.实际用途
//巧用swap可以收缩内存空间
void test02()
{
	vector<int>v3;
	for (int i = 0; i < 10000; i++)
	{
		v3.push_back(i);
	}
	cout << "v3的容量为:" << v3.capacity() << endl;
	cout << "v3的大学为:" << v3.size() << endl;
	//重新指定大小
	v3.resize(10);
	cout << "v3的容量为:" << v3.capacity() << endl;
	cout << "v3的大学为:" << v3.size() << endl;
	//可以发现,这里得到容量被大量浪费了
	//可以巧用swap收缩内存
	vector<int>(v3).swap(v3);
	cout << "v3的容量为:" << v3.capacity() << endl;
	cout << "v3的大学为:" << v3.size() << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

The following is the result of the operation:

Before swap:
0 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1
After swap:
10 9 8 7 6 5 4 3 2 1 0 1 2
3 4 5 6 7 8 9
The capacity of v3 is : 12138
University for v3: 10000
Capacity for v3: 12138
University for
v3: 10 Capacity for v3: 10
University for v3: 10
Press any key to continue. . .

4.8 vector reserved space

Function description:  reduce the number of expansions of vector when dynamically expanding capacity

Function prototype:  reserve(int len); //The container reserves the length of len elements, the reserved position is not initialized, and the elements are inaccessible.

Five, deque container

5.1 Basic concepts

Function: double-ended array, which can insert and delete the head end

The difference between deque and vector :

1. The insertion and deletion efficiency of vector is low for the header , the larger the data volume, the lower the efficiency

2. Relatively speaking, deque will insert and delete the head faster than vector

3. When vector accesses elements, it will be faster than deque, which is related to the internal implementation of the two

 The picture comes from the video lecture notes of Dark Horse Programmer

The inner working principle of deque :

There is a central controller inside the deque, which maintains the contents of each buffer, and stores real data in the buffer. The central controller maintains the address of each buffer, making it look like a continuous memory space when using deque.

5.2 deque constructor

Function description: deque container construction

Function prototype:

1. deque<T> deqT; //Default construction form

2. deque(beg, end); //The constructor copies the elements in the range [beg, end) to itself.

3. deque(n, elem); //The constructor copies n elems to itself.

4. dequé(const deque &deq); //copy constructor

Summary:  The construction methods of deque containers and vector containers are almost the same.

5.3 Deque container assignment

Function prototype:
1. deque& operator=(const deque &deq); //overload equal sign operator

2. assign( beg, end ) ; //Assign the copy of the data in the range [beg, end) to itself.

3. assign(n, elem); //Assign n copies of elem to itself.

5.4 deque size operation

Function description:  operate on the size of the deque container

Function principle:

deque.empty(); //Determine whether the container is empty

deque.size(); //returns the number of elements in the container

deque.resize(num); //Respecify the length of the container as num, if the container becomes longer, fill the new position with the default value. If the container gets shorter, elements at the end that exceed the length of the container are removed.

deque.resize(num, elem); //Respecify the length of the container as num, if the container becomes longer, fill the new position with the value of elem. If the container gets shorter, elements at the end that exceed the length of the container are removed.

5.5 Insertion and deletion of deque containers

Function prototype:

deque.empty(); //Determine whether the container is empty

deque.size(); //returns the number of elements in the container

deque.resize(num); //Respecify the length of the container as num, if the container becomes longer, fill the new position with the default value. If the container gets shorter, elements at the end that exceed the length of the container are removed.

deque.resize(num, elem);//Respecify the length of the container as num, if the container becomes longer, fill the new position with the value of elem. If the container gets shorter, elements at the end that exceed the length of the container are removed.

5.6 Data access of deque container

Function prototype:

at(int idx); //return the data pointed by the index idx

operator[];I/returns the data pointed to by the index idx front(); //returns the first data element in the container

back(); //returns the last data element in the container

5.6 Insertion and deletion of deque container

algorithm:

sort(iterator beg, iterator end) //Sort the elements in the range of beg and end.

Guess you like

Origin blog.csdn.net/m0_74893211/article/details/131344598