STL container value vector and deque detailed explanation

Insert picture description here

Source: WeChat Official Account "Programming Learning Base"

vector

Vector is a sequential container that encapsulates a dynamic size array

Sequence sequence

  • The elements in the sequence container are sorted in strict linear order. The corresponding element can be accessed by its position in the sequence.

Dynamic array

  • It supports quick and direct access to any element in the sequence, and even this operation can be performed through pointer calculation.

Basic function

Constructor

vector():创建一个空vector
vector(int nSize):创建一个vector,元素个数为nSize
vector(int nSize,const t& t):创建一个vector,元素个数为nSize,且值均为t
vector(const vector&):复制构造函数
vector(begin,end):复制[begin,end)区间内另一个数组的元素到vector中

Increase function

void push_back(const T& x):向量尾部增加一个元素X
iterator insert(iterator it,const T& x):向量中迭代器指向元素前增加一个元素x
iterator insert(iterator it,int n,const T& x):向量中迭代器指向元素前增加n个相同的元素x
iterator insert(iterator it,const_iterator first,const_iterator last):向量中迭代器指向元素前插入另一个相同类型向量的[first,last)间的数据

Delete function

iterator erase(iterator it):删除向量中迭代器指向元素
iterator erase(iterator first,iterator last):删除向量中[first,last)中元素
void pop_back():删除向量中最后一个元素
void clear():清空向量中所有元素

Traverse function

reference at(int pos):返回pos位置元素的引用
reference front():返回首元素的引用
reference back():返回尾元素的引用
iterator begin():返回向量头指针,指向第一个元素
iterator end():返回向量尾指针,指向向量最后一个元素的下一个位置
reverse_iterator rbegin():反向迭代器,指向最后一个元素
reverse_iterator rend():反向迭代器,指向第一个元素之前的位置

Judgment function

bool empty() const:判断向量是否为空,若为空,则向量中无元素

Size function

int size() const:返回向量中元素的个数
int capacity() const:返回当前向量所能容纳的最大元素值
int max_size() const:返回最大可允许的vector元素数量值

Other functions

void swap(vector&):交换两个同类型向量的数据
void assign(int n,const T& x):设置向量中第n个元素的值为x
void assign(const_iterator first,const_iterator last):向量中[first,last)中元素设置成当前向量元素

Simple to use

#include <string.h>
#include <vector>
#include <iostream>
using namespace std;

int main()
{
    
    
	vector<int> obj;	
	//添加数据
	for (int i = 0; i<10; i++) 
	{
    
    
		obj.push_back(i);
	}
	//删除部分数据
	for (int i = 0; i<5; i++)
	{
    
    
		obj.pop_back();
	}
	cout << "\n" << endl;
	//下标访问所有数据
	for (int i = 0; i<obj.size(); i++)
	{
    
    
		cout << obj[i] << ",";
	}
	return 0;
}

Sort

head File

#include <algorithm>

function

sort(iterator first,iterator last);		//从小到大
reverse(iterator first,iterator last);	//从大到小

Example

#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
bool compare(int a, int b)
{
    
    
	return a> b; //升序排列,如果改为return a>b,则为降序 
}
int main()
{
    
    
	vector<int>obj;
	obj.push_back(1);
	obj.push_back(3);
	obj.push_back(0);

	sort(obj.begin(), obj.end());//从小到大

	cout << "从小到大:" << endl;
	for (int i = 0; i<obj.size(); i++)
	{
    
    
		cout << obj[i] << ",";
	}

	cout << "\n" << endl;

	cout << "从大到小:" << endl;
	//reverse(obj.begin(), obj.end());//从大到小 
	sort(obj.begin(), obj.end(), compare);//从小到大
	for (int i = 0; i<obj.size(); i++)
	{
    
    
		cout << obj[i] << ",";
	}
	return 0;
}

Iterator traversal

#include <vector>
#include <iostream>
using namespace std;

int main()
{
    
    
	//顺序访问
	vector<int>obj;
	for (int i = 0; i<10; i++)
	{
    
    
		obj.push_back(i);
	}

	cout << "直接利用数组:";
	for (int i = 0; i<10; i++)//方法一 
	{
    
    
		cout << obj[i] << " ";
	}

	cout << endl;
	cout << "利用迭代器:";
	//方法二,使用迭代器将容器中数据输出 
	vector<int>::iterator it;//声明一个迭代器,来访问vector容器,作用:遍历或者指向vector容器的元素 
	for (it = obj.begin(); it != obj.end(); it++)
	{
    
    
		cout << *it << " ";
	}
	return 0;
}

Two definition methods of two-dimensional array

#include <vector>
#include <iostream>
using namespace std;

int main()
{
    
    
	int N = 5, M = 6;
	vector<vector<int> > obj(N); //定义二维动态数组大小5行 
	for (int i = 0; i< obj.size(); i++)//动态二维数组为5行6列,值全为0 
	{
    
    
		obj[i].resize(M);
	}

	for (int i = 0; i< obj.size(); i++)//输出二维动态数组 
	{
    
    
		for (int j = 0; j<obj[i].size(); j++)
		{
    
    
			cout << obj[i][j] << " ";
		}
		cout << "\n";
	}
	return 0;
}

and

The deque container performs linear processing for a given type of element . Like a vector, it can quickly and randomly access any element , and can efficiently insert and delete the tail elements of the container. But it is different from vector, deque supports efficient insertion and deletion of the head element of the container , so it is also called double-ended queue *. The commonly used functions of the deque class are as follows.

Basic function

Constructor

deque()://创建一个空deque

deque(int nSize)://创建一个deque,元素个数为nSize

deque(int nSize,const T& t)://创建一个deque,元素个数为nSize,且值均为t

deque(const deque &)://拷贝构造函数

Increase function

void push_front(const T& x)://双端队列头部增加一个元素X

void push_back(const T& x)://双端队列尾部增加一个元素x

iterator insert(iterator it,const T& x)://双端队列中某一元素前增加一个元素x

void insert(iterator it,int n,const T& x)://双端队列中某一元素前增加n个相同的元素x

void insert(iterator it,const_iterator first,const_iteratorlast)://双端队列中某一元素前插入另一个相同类型向量的[forst,last)间的数据

Delete function

Iterator erase(iterator it)://删除双端队列中的某一个元素

Iterator erase(iterator first,iterator last)://删除双端队列中[first,last)中的元素

void pop_front()://删除双端队列中最前一个元素

void pop_back()://删除双端队列中最后一个元素

void clear()://清空双端队列中最后一个元素

Traverse function

reference at(int pos)://返回pos位置元素的引用

reference front()://返回首元素的引用

reference back()://返回尾元素的引用

iterator begin()://返回向量头指针,指向第一个元素

iterator end()://返回指向向量中最后一个元素下一个元素的指针(不包含在向量中)

reverse_iterator rbegin()://反向迭代器,指向最后一个元素

reverse_iterator rend()://反向迭代器,指向第一个元素的前一个元素

Judgment function

bool empty() const://向量是否为空,若true,则向量中无元素

Size function

int size() const://返回向量中元素的个数

int max_size() const://返回最大可允许的双端对了元素数量值

Other functions

void swap(deque&)://交换两个同类型向量的数据

void assign(int n,const T& x)://向量中第n个元素的值设置为x

Comparison of deque and vector memory allocation

#include<iostream>  
#include<deque>  
#include<vector>  

using namespace std;
int main()
{
    
    
	vector<int>v(2);
	v[0] = 10;
	int* p = &v[0];
	cout << "vector第一个元素迭代指针*p=" << *p << endl;
	v.push_back(20);
	cout << "vector容量变化后原vector第1个元素迭代指针*p=" << *p << endl;

	deque<int> d(2);
	d[0] = 10;
	int* q = &d[0];
	cout << "deque第一个元素迭代指针*q=" << *q << endl;
	d.push_back(20);
	cout << "deque容量变化后第一个元素迭代器指针*q=" << *q << endl;
}
vector第一个元素迭代指针*p=10
vector容量变化后原vector第1个元素迭代指针*p=-572662307
deque第一个元素迭代指针*q=10
deque容量变化后第一个元素迭代器指针*q=10

The function of this program is: deque and vector are initialized with a size of 2, and the first element is both 10. When an element is added to the two containers through the push_back function, it is found from the result that the value of the pointer element that was originally held is paired with the vector container There has been a change before and after, and there is no change before and after the deque container . The reason is that when creating a vector container, generally speaking, create space -> fill data -> rebuild larger space -> copy the original space data -> delete the original space -> add new data , and repeat it to ensure that the vector is always an independent contiguous memory space ; when establishing deque container, generally will be with the establishment of space -> Create data -> to create a new space -> fill new data , so again, no copy, and delete the original process spatial data , by more than Consecutive memory space .

After using erase(itertor), for sequence container vector, deque, after using erase(itertor), the iterator of each element behind will be invalid, but each element will move forward by one position, but erase will return Next valid iterator

Guess you like

Origin blog.csdn.net/qq_44519484/article/details/110427572