Come to learn STL (1) <vector> use (continuous update)

Series of articles catalog


大 vvvvvv

来学STL(1)<vector>使用初阶(持续更新)
按需求抓重点。收藏起来

Description

This article focuses on explaining the commonly used methods, the folk remedies are easy to mess with me.
Original article, reprinting without permission is prohibited

Definition of vector container

Vector is a sequence container (Sequence Container) that encapsulates a dynamically sized array. Like any other type of container, it can store various types of objects. It can be simply considered that a vector is a dynamic array that can store any type. (From the rookie tutorial)

My understanding is that it is an array, an array of indefinite length. The length of this array is not fixed, it is as simple as that of using it as required.

Simple code usage of vector container (original code)

This code can briefly look at the idea of ​​using vector. For details, please jump on demand

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	vector<int> v;//创建一个容器,也可以说是数组 
	v.push_back(0);// 如果当做数组理解 就是v[0]=0,就是加入0元素
	v.push_back(1);//v[1] = 1; 
	v.push_back(3);
	v.push_back(4);
	//让我们输出看一看吧 
	for(int i = 0;i<4;i++){
    
    
		cout<<v[i]<<endl; //加入元素虽然用push_back,但是取值遍历像数组一样使用 
	}
	return 0;
} 

operation result

0
1
3
4

Construction and assignment traversal of vector container (apply for a vector container)

The most commonly used construction method is the original code, of course there are others. You can check this article
==>> Poke me directly.
If you don’t understand, then look at the following code (simplified)

vector<int> v1();		//创造一个空v1,小括号爱要不要,看脸 
	vector<int> v2(10);		//这个就是直接申请了一个初始长度为10的数组,但是与普通数组不同的是它还可以加元素 
	vector<int> v3(10,0);	//申请空间为10,并把这10个空间初始化为0 还挺好用 
	vector<int> v4(v3);		//复制所有的元素 就是对v4赋值了,只不过赋值的是一个数组,STL的独特操作哈哈,也可以v3=v4。
	 
	vector<int> v5(v3.begin(),v3.begin()+5); // 将v3的开始到第五个元素复制到v5中。 
	 

Assignment:
push_back() function -I don't like to move the definition, just master the usage method.
It is easy to know by looking at the name, it will add a number to the back of the array (for professionalism, apply for a space first, and then add the number).
Then someone will ask, what if we add a number in the middle.
inset() function -insert a number. You can insert a number in the specified position.
Use method, and put the code if you don't agree.
Here only introduce a few commonly used and convenient methods, because I am dizzy because of the trouble.

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	vector<int> v;//创建一个容器,也可以说是数组 
	v.push_back(0);// 如果当做数组理解 就是v[0]=0,就是加入0元素
	v.push_back(1);//v[1] = 1; 
	v.push_back(3);
	v.push_back(4);
	//让我们输出看一看吧 
	for(int i = 0;i<4;i++){
    
    
		cout<<v[i]<<endl; //加入元素虽然用push_back,但是取值遍历可以像数组一样使用 
	}
	//inset(要插入元素的地址,插入元素的值) ; 
	
	v.insert(v.begin(),2);	 //在最前面插入2,也就是说 v[0]=2了。
	
	//v.begin()是数组的起始地址,v.end()是结尾地址。 
	
	for(int i = 0;i<5;i++){
    
     // 5?? 插入了一个元素,长度不就变5了。
		cout<<v[i]<<' ';
	}
	cout<<endl; 
	
	
	v.insert(v.begin()+2,5);	 //在第二个位置插入位置5,v[2]=5;
	
	for(int i = 0;i<6;i++) cout<<v[i]<<' ';
	cout<<endl; 
	return 0;
} 

operation result

0
1
3
4
2 0 1 3 4
2 0 5 1 3 4

Note that inset operates at the address level. The first parameter is the address, and the second is the number you want to insert. That is, if you want a large number of inserts, the simple way is to use an iterator (updated later), in short, you tell this function-what do I want to insert at the "address"---- place---- "element"----
I don't like inset, because it means jump in line (hehe~~~).
Traversal function
Traversal function is the simplest array traversal, which is the usage of arrays. You can understand the original code's for loop.
There is also an iterator traversal, just know the iterator. Don't go into details, just comment if needed.

Get length, delete and sort of vector container

1. Get the length
size() function can get the size of the container (array).
Very easy to understand, look at the code directly

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	vector<int> v;//创建一个容器,也可以说是数组 
	v.push_back(0);// 如果当做数组理解 就是v[0]=0,就是加入0元素
	v.push_back(1);//v[1] = 1; 
	v.push_back(3);
	v.push_back(4);
	//让我们输出看一看吧 
	for(int i = 0;i<4;i++){
    
    
		cout<<v[i]<<endl; //加入元素虽然用push_back,但是取值遍历可以像数组一样使用 
	}
	//把上面的4改成length() 意味着 size()=4 
	for(int i = 0;i<size();i++){
    
    
		cout<<v[i]<<endl;
	}
	return 0;
}

operation result:

0
1
3
4
0
1
3
4
v.size() = 4

2. Delete :
1. Delete the entire container : clear() function. Clear the number in the vector directly. Note that he will not retreat the room, but will not release the space. If you release the space, you can use swap(). It’s fine if you know it. You don’t use much. Look at the code in detail.

	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	cout<<v.size()<<endl;
	v.clear();
	cout<<v.size()<<endl;
	vector<int>().swap(v); //知道就好,用的不多,
	//因为clear()之后size就在0,
	//所以只用个clear就可以以为是清空了
	cout<<v.size()<<endl;

operation result

2
0
0

*2. Delete an element: *There is an inset() for cutting the line, and there is also a function
erase() for kicking out (deleting) an element to delete an element.

The function prototype of erase has two forms:
iterator erase(iterator position);
iterator erase(iterator first, iterator last); The
first is to delete the element at a single position, that is to say-"delete this element for me" and the
second One is to delete elements in consecutive positions, that is to say-"delete me the elements from first to last"

It is not difficult to understand, the code:

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	vector<int> v;//创建一个容器,也可以说是数组 
	v.push_back(0);// 如果当做数组理解 就是v[0]=0,就是加入0元素
	v.push_back(1);//v[1] = 1; 
	v.push_back(3);
	v.push_back(4);
	//让我们输出看一看吧 
	for(int i = 0;i<4;i++){
    
    
		cout<<v[i]<<endl; //加入元素虽然用push_back,但是取值遍历可以像数组一样使用 
	}
	v.erase(v.begin()); //v[0]的元素
	
	for(int i = 0;i<v.size();i++) cout<<v[i]<<' ';
	
	cout<<endl; 
	
	v.erase(v.begin(),v.begin()+2);//删除v[0]到v[2]的函数
	
	 for(int i = 0;i<v.size();i++) cout<<v[i]<<' ';
	 
	 cout<<endl;
	 
	cout<<"v.size() = "<<v.size()<<endl;
	
	v.erase(v.begin(),v.end()); //删除所有元素
	
	cout<<"v.size() = "<<v.size()<<endl;
	return 0;
} 

operation result

0
1
3
4
1 3 4
4
v.size() = 1
v.size() = 0

Note: Same as inset(), it operates at the address level. If you delete a specific element to delete, you can use an iterator to traverse and delete, or do you have a better way?
3.sort sorting
is essentially the same as array sorting, both are passed to the sort() function address, but the way of writing is different, the code is as follows, with instructions

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	int a[3];
	vector<int> v;
	a[0] = 2; 
	v.push_back(2);
	
	a[1] = 3;
	v.push_back(3);
	
	a[2] = 1;
	v.push_back(1); 
	
	sort(a,a+3);	//数组排序  
	
	sort(v.begin(),v.end());	//stl容器排序 
	
	//如果v写出sort(v,v+3)不能通过编译,因为其不能代表地址,数组的a与a+3是代表地址的 
	
	for(int i = 0;i<3;i++) cout<<a[i]<<' ';
	
	cout<<endl;
	
	for(int i = 0;i<v.size();i++) cout<<v[i]<<' ';
	
	return 0;
}

operation result:

1 2 3
1 2 3

Continuously updating...

Guess you like

Origin blog.csdn.net/eatkeyborad/article/details/114108425