Introduction to the usage of C++ dynamic array <vector>

Advantages of vector arrays :
Compared with ordinary arrays, there is no need to define the length of the array, and the length can be changed, saving space, can store any object, can copy and assign, and can increase the length beyond the length.

Header file: #include<vector>

Construct declaration : vector<type> array name

For example: vector<int>a
defines a dynamic array a that stores integers

insert image description here

Specific operation:

1. Insert element :
array name.push_back(element);

2. Get the length of the array :
array name.size()
Example:

#include<bits/stdc++.h>
using namespace std;
vector<int> a;
int main()
{
    
    
	a.push_back(1);//插入元素1
	a.push_back(2);//插入元素2
	a.push_back(3);//插入元素3
	a.push_back(4);//插入元素4
	for(int i=0;i<a.size();i++)//获取数组长度
	printf("%d ",a[i])//输出数组各元素
	return 0;
} 

Note : The subscript of a dynamic array starts from a[0] like a normal array, and the access method can be the same as that of a normal array

3. Change array elements:
array name [subscript] = new element
Example:

#include<bits/stdc++.h>
using namespace std;
vector<int> a;
int main()
{
    
    
	a.push_back(1);
	a.push_back(2);
	a.push_back(3);
	a.push_back(4);
	a[2]=0;//改变元素
	for(int i=0;i<a.size();i++)
	printf("%d ",a[i]);
	return 0;
} 

result:
insert image description here

4. Delete elements (only the end can be deleted)
array name.pop_back();
Example:

#include<bits/stdc++.h>
using namespace std;
vector<int> a;
int main()
{
    
    
	a.push_back(1);
	a.push_back(2);
	a.push_back(3);
	a.push_back(4);
	a.pop_back();//删除末端元素
	for(int i=0;i<a.size();i++)
	printf("%d ",a[i]);
	return 0;
} 

Result:
insert image description here
5. Clear
the array name.clear(); (only clears the vertor, not the memory)
Example:

#include<bits/stdc++.h>
using namespace std;
vector<int> a;
int main()
{
    
    
	a.push_back(1);
	a.push_back(2);
	a.push_back(3);
	a.push_back(4);
    a.clear();//清空所有元素
	for(int i=0;i<a.size();i++)
	printf("%d ",a[i]);
	return 0;
} 

Result:
insert image description here
7. Array initialization
Declaration: vector <type> array name (initialization length, initial number)

For example:
int n=0;
vector< int >a(1000,n);
initialize the array to zero

Example:

#include<cstdio>
#include<vector>
using namespace std;
int main()
{
    
    
	int n;
	vector<int>a(10,1);//初始化为1
	for(int i=0;i<a.size();i++)
	printf("%d ",a[i]);
	return 0;
}

Result:
insert image description here
another initialization (for a two-bit vector):
vector< int > a[maxn];

int m,n;
for(int i=0;i<m;i++){
    
    
      a[i].resize(n);
}

8. Delete the specified element
using iterator to delete

#include<bits/stdc++.h>
using namespace std;
vector<int> ve;
int main(){
    
    
	ve.push_back(1);
	ve.push_back(2);
	ve.push_back(3);
	for(vector<int>::iterator it = ve.begin(); it !=ve.end(); it ++){
    
    
		if(*it==2){
    
    //删除元素2 
			ve.erase(it);
			break;
		}
	}
	for(int i = 0; i < ve.size(); i ++ ){
    
    
		cout<<ve[i]<<endl;
	} 
	
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324897063&siteId=291194637