SoFunction
Updated on 2025-04-14

A very detailed explanation of common usage of C++ vector

1. Definition of vector

Define a vector separately:

vector<typename> name;

The above definition is actually equivalent to a one-dimensional array name[size], but its size can be changed according to needs. This is the origin of the name of "variable length array".

The typename here can be any basic type, such as int, double, char, structure, etc., or it can be an STL standard container, such as: set, queue, vector, etc.

Next is the usage of a two-dimensional vetor array:

vector<typename> Arrayname[size]; 

Among them, each element in Arrayname[] is a vector. We can understand the two-dimensional vector array as a two-dimensional array with both dimensions that can be side-length.

For example:

vector&lt;int&gt; vi[100];
//vi[0] ~ vi[100 - 1]Each one is onevectorcontainer

2. Common initialization methods for vector

1. Use curly braces to assign values ​​directly

vector&lt;int&gt; v{1,2,3,4,5}; // Directly use curly braces to assign valuesfor(auto i : v) cout &lt;&lt; i &lt;&lt; " "; //Output1 2 3 4 5

2. Assign values ​​using parentheses

vector&lt;int&gt; v(5); //Initialize 5 elements with value 0for(auto i : v) cout &lt;&lt; i &lt;&lt; " "; //Output0 0 0 0 0
vector&lt;int&gt; v(5, 4); //Initialize 5 elements with a value of 4for(auto i : v) cout &lt;&lt; i &lt;&lt; " "; //Output4 4 4 4 4

3. Access to elements in vector container

1. Access through subscript:

A vector container defined as a vector<int> v can be accessed using v[0], v[1],... Of course, the subscript cannot exceed the bounds (within ()-1)

Note: v[0], v[1], v[2] is equivalent to *(() ), *(() + 1), *(() + 2).

2. Access through an iterator:

Iterator can be understood as something like a pointer, and its definition is:

vector<typename>::iterator it;

This gets the iterator it, and the elements in the vector can be accessed through *it:

Access method:

#include&lt;bits/stdc++.h&gt;
using namespace std;
int main(){
	vector&lt;int&gt; v;
	v.push_back(1);	//Enter 1, 2, 3 in turn	v.push_back(2);
	v.push_back(3);
	for(vector&lt;int&gt;::iterator it = (); it != (); it++){
		cout &lt;&lt; *it &lt;&lt; " ";
	}
	
    return 0;
} 

Output results: 1, 2, 3

4. Analysis of common function instances of vector

1、push_back():

v.push_back(x), which means adding an element x after vector container v, the time complexity is O(1);

#include<bits/stdc++.h>
using namespace std;
int main(){
	vector<int> v;
	for(int i = 1; i <= 3; i++){
		v.push_back(i);
	}
	for(vector<int>::iterator it = (); it != (); it++){
		cout << *it << " ";
	}
	return 0;
} 

2、pop_back():

pop_back() can delete the tail element of the vector, and the time complexity is O(1):

#include&lt;bits/stdc++.h&gt;
using namespace std;
int main(){
	vector&lt;int&gt; v;
	for(int i = 1; i &lt;= 3; i++){
		v.push_back(i);	//Insert 1, 2, and 3 into the end of the team of v in sequence	}
	v.pop_back();	//Delete the tail element	for(vector&lt;int&gt;::iterator it = (); it != (); it++){
		cout &lt;&lt; *it &lt;&lt; " ";
	}
	
    return 0;
} 

Output 1 2

3、size():

size() is used to get the number of elements in the vector. The time complexity is O(1). size() returns an unsigned type:

#include&lt;bits/stdc++.h&gt;
using namespace std;
int main(){
	vector&lt;int&gt; v;
	for(int i = 1; i &lt;= 3; i++){
		v.push_back(i);	//Insert 1, 2, and 3 into the end of the team of v in sequence	}
	v.pop_back();	//Delete the tail element	//for(vector&lt;int&gt;::iterator it = (); it != (); it++){
//		cout &lt;&lt; *it &lt;&lt; " ";
//	}
	cout &lt;&lt; ();    //Get the container length 
    return 0;
} 

4、clear():

clear() is used to clear all elements in vector, with a time complexity of o(N).

#include&lt;bits/stdc++.h&gt;
using namespace std;
int main(){
	vector&lt;int&gt; v;
	for(int i = 1; i &lt;= 3; i++){
		v.push_back(i);	//Insert 1, 2, and 3 into the end of the team of v in sequence	}
	v.pop_back();	//Delete the tail element	();	
	//for(vector&lt;int&gt;::iterator it = (); it != (); it++){
//		cout &lt;&lt; *it &lt;&lt; " ";
//	}
	cout &lt;&lt; ();
	
    return 0;
} 

5、insert():

insert(it,x) is used to insert an element x to any iterator of the vector.

#include&lt;bits/stdc++.h&gt;
using namespace std;
int main(){
	vector&lt;int&gt; v;
	for(int i = 1; i &lt;= 5; i++){
		v.push_back(i);	//Insert 1, 2, 3, 4, and 5 into the end of the team of v in sequence	}
	(() + 2, -1);	//Insert -1 into the position of v[2]	for(vector&lt;int&gt;::iterator it = (); it != (); it++){
		cout &lt;&lt; *it &lt;&lt; " ";
	}
	
	return 0; 
} 

1 2 -1 3 4 5

 insert(pos,first,last) existposBefore positioning, insert all elements in the [first, last) area in other containers (not limited to vectors) (simply put, splice the two containers together).

#include&lt;bits/stdc++.h&gt;
using namespace std;
 
int main(){
	vector&lt;int&gt; v, v1, v2;
    for(int i = 1; i &lt;= 3; i++) v.push_back(i);
	for(int i  = 11; i &lt;= 13; i++) v1.push_back(i);
	for(int i  = 101; i &lt;= 103; i++) v2.push_back(i);
	((), (), ());  //Output 1 2 3 11 12 13	((), (), ()); //Output 1 2 3 11 12 13 101 102 103 
    return 0;
}

6、erase()

erase()There are two uses: delete a single element, delete all elements in a range. The time complexity is O(N).

①Delete a single element:

erase(it)That is, delete the iterator asitElements at:

#include&lt;bits/stdc++.h&gt;
using namespace std;
int main(){
	vector&lt;int&gt; v;
	for(int i = 1; i &lt;= 5; i++){
		v.push_back(i);	//Insert 1, 2, 3, 4, and 5 into the end of the team of v in sequence	}
	(() + 2);	//Delete 3	for(vector&lt;int&gt;::iterator it = (); it != (); it++){
		cout &lt;&lt; *it &lt;&lt; " ";
	}
	
	return 0; 
} 

Output 1 2 4 5

②Delete all elements in an interval:

erase(first, last)Delete[first, last)All elements within:

#include&lt;bits/stdc++.h&gt;
using namespace std;
int main(){
	vector&lt;int&gt; v;
	for(int i = 1; i &lt;= 5; i++){
		v.push_back(i);	//Insert 1, 2, 3, 4, and 5 into the end of the team of v in sequence	}
	(() + 1, () + 3);	//Delete 2 and 3	for(vector&lt;int&gt;::iterator it = (); it != (); it++){
		cout &lt;&lt; *it &lt;&lt; " ";
	}
	
	return 0; 
} 

Output 1 4 5

If you want to use erase to clear the vector container, the correct way to write it is ((), ())()It represents the tail elementNextaddress.

Summarize

This is the article about this article that explains the common usage of C++ vectors in a very detailed way. For more common usage of C++ vectors, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!