Introduction to c++ vector usage must-see super detailed

1. The role of vector

Vector is one of the most commonly used containers, which is very powerful and can store and manage various types of data. In many cases, it can be used to replace ordinary arrays with limited functions, because we know that ordinary arrays can only achieve one-to-one mapping but not one-to-many mapping. Vector was born specifically to solve this problem. Vector can also be called a dynamic array, because its size changes according to real-time updates, and because of this, vector is more flexible and easy to use.

For example,
Xiao Ming owns a banana and two apples, and
Xiao Fang owns a lollipop and three pears
. Then try to think about how to represent this structure in the program? This is an example of one-to-many mapping, which can be easily solved after learning vector.

2. The definition of vector

vector<storage type> container name
For example:
store int type value vector<int> v;
store double type value vector<double> v;
store string type value vector<string> v;
store structure or class value Value vector<structure name> v;

Of course, you can also define a vector array:
store an int value vector<int> v[n];
store a double value vector<double> v[n];
etc., n is the size of the array

3. Commonly used member functions of vector

//这些都是必会的成员函数
size()//返回返回容器中元素个数
begin()//返回头部迭代器
end()//返回尾部+1迭代器
rbegin()//返回逆首部迭代器
rend()//返回逆尾部-1迭代器
front()//返回首个元素
back()//返回尾部元素
push_back()//在末尾添加一个函数
emplace_back()//和push_back()是一样的作用
pop_back()//弹出最后一个元素
empty()//判断是否为空
insert()//在指定位置插入元素
erase()//在指定位置删除元素
clear()//清空容器

Detailed analysis:
(1) size()
size() is the most commonly used member function, and is often used for vector traversal and element number query


(2) In addition to the initialization of push_back(), push_back() is the most important modification function of vector, and the complexity is O(1), while the complexity of insert() is O(n), the gap is obvious

(3) front(), back()
query the first element and the last element, in fact, through random access, v[0], v[v.size()-1] can achieve the same effect, use front() and back ( ) look professional

(4) begin() and end()
are usually used for convenient sorting, and can also be used for traversal, but it is not necessary. Traversing through subscripts is simpler and faster, while rbegin() and rend() can be used for reverse order sorting

(5) insert()
insert (position, x );
insert (position, n, x );
insert (position, first, last );
insert a new element,
the first function, insert a value before the position specified by the iterator
The second function inserts n elements whose value is x before the position specified by the iterator The third
function inserts a sequence of another container before the position specified by the iterator Iterator first to last
Complexity Very high, have to not use

(6) erase()
erase ( position );
erase (first, last );
delete an element or a sequence
Similarly, the complexity is very high, so it has to be used

Sample code:

#include<iostream>//c++标准头文件,可以使用cout,cin等标准编译用法
#include<vector>//使用vector时需要的头文件 
#include<algorithm>//包含许多内置函数,如排序、倒置、交换等函数 
using namespace std;//命名空间,防止重名给程序带来各种隐患,使用cin,cout,map,set,vector,queue时都要使用
//遍历函数 
void print(auto v){
    
    
	for(auto c:v){
    
    
		cout<<c<<' ';
	}
	cout<<endl;
}
int main(){
    
    
	vector<int> v;//定义一个 int型vector 
	
	v.emplace_back(3);
	v.emplace_back(4);
	v.emplace_back(1);
	v.emplace_back(2);
	cout<<"现在有的元素 :";
	print(v);
	cout<<endl;
	
	cout<<"v.front()="<<v.front()<<endl;
	cout<<"v.back()="<<v.back()<<endl;
	cout<<endl;
	
//	排序
	sort(v.begin(),v.end());
	cout<<"排序后 :";
	print(v);
	cout<<"v.front()="<<v.front()<<endl;
	cout<<"v.back()="<<v.back()<<endl;
	cout<<"v.size()="<<v.size()<<endl;
	cout<<endl;
	
//	插入5、6 
	v.insert(v.begin()+2,{
    
    5,6}); 
	cout<<"插入5、6后: ";
	print(v);
	cout<<endl;
	
//	倒置
	reverse(v.begin(),v.end());
	cout<<"倒置后: ";
	print(v);
	cout<<endl;
	
//	逆序排序 
	sort(v.rbegin(),v.rend());
	cout<<"逆序排序后 :";
	print(v);
	
//	判断是否为空
	cout<<"v.empty()="<<v.empty()<<endl; 
	cout<<"(v.size()==0)="<<(v.size()==0)<<endl;
	cout<<endl;
	
	v.clear();
	cout<<"v清空后"<<endl; 
	cout<<"v.empty()="<<v.empty()<<endl; 
	cout<<"(v.size()==0)="<<(v.size()==0)<<endl;
} 

operation result:

现在有的元素 :3 4 1 2

v.front()=3
v.back()=2

排序后 :1 2 3 4
v.front()=1
v.back()=4
v.size()=4

插入56: 1 2 5 6 3 4

倒置后: 4 3 6 5 2 1

逆序排序后 :6 5 4 3 2 1
v.empty()=0
(v.size()==0)=0

v清空后
v.empty()=1
(v.size()==0)=1

4. Three traversal methods of vector

(1) iterator iterator

code:

#include<iostream>
#include<vector>
using namespace std;
int main(){
    
    
	vector<int> v;//定义 
	v.emplace_back(1);//插入元素1 
	v.emplace_back(3);//插入元素3
	v.emplace_back(2);//插入元素2
	vector<int>::iterator it;//使用迭代器
	for(it=v.begin();it!=v.end();it++){
    
    
		cout<<*it<<' ';
	} 
} 

operation result:

1 3 2

(2) Subscript traversal

code:

#include<iostream>
#include<vector>
using namespace std;
int main(){
    
    
	vector<int> v;//定义 
	v.emplace_back(1);//插入元素1 
	v.emplace_back(3);//插入元素3
	v.emplace_back(2);//插入元素2
	for(int i=0;i<v.size();i++){
    
    
		cout<<v[i]<<' ';
	}
} 

operation result:

1 3 2

(3) foreach traversal

code:

#include<iostream>
#include<vector>
using namespace std;
int main(){
    
    
	vector<int> v;//定义 
	v.emplace_back(1);//插入元素1 
	v.emplace_back(3);//插入元素3
	v.emplace_back(2);//插入元素2
	for(int c:v){
    
    
		cout<<c<<' ';
	}
} 

operation result:

1 3 2

Two kinds of reverse order traversal:
code:

#include<iostream>
#include<vector>
using namespace std;
int main(){
    
    
	vector<int> v;//定义 
	v.emplace_back(1);//插入元素1 
	v.emplace_back(3);//插入元素3
	v.emplace_back(2);//插入元素2
	
//	迭代器法 
	vector<int>::reverse_iterator it;//使用迭代器
	for(it=v.rbegin();it!=v.rend();it++){
    
    
		cout<<*it<<' ';
	} 
	cout<<endl;
	
//	下标遍历法
	for(int i=v.size()-1;i>=0;i--){
    
    
		cout<<v[i]<<' ';
	} 
} 

operation result:

2 3 1
2 3 1

Although foreach is simple and easy to use, it does not support reverse order traversal

Some friends found that dev cannot compile part of the code, that is because dev has not yet supported c++11, you can read this article to solve
dev using c++11 tutorial

Is it very simple?

It will definitely be difficult for newcomers. Do more questions and use them more. Once you get familiar with it, it will be easy. Brother Meng, come on! ! !

There are still deficiencies in the article, welcome to correct me

Thanks for watching, like it

Guess you like

Origin blog.csdn.net/weixin_52115456/article/details/126024253