C + + "STL vector Learning Notes"


The vector class is called the vector class. It implements a dynamic array, which is used for an array of objects with variable number of elements.

The elements in the order container are sorted in strict linear order. The corresponding element can be accessed through the position of the element in the sequence.

When expanding a new element, if it exceeds the current capacity, the capacity will be automatically expanded by 2 times. If the capacity is still insufficient, it will continue to expand by 2 times.


vector<object_type> v1;       

Vector (): create an empty vector.
Vector (itn nSize): create a vector with the number of elements being nSize.
Vector (int nSize, const T & T): create a vector, the number of elements is nSize, and the value is t.
Vector (const vector &): copy constructor.

vector<int> vec;		//Declare a vector of type int

vector<int> vec(5);		//Declare an int vector with an initial size of 5

vector<int> vec(10, 1);	//Declare a vector with an initial size of 10 and values of 1

vector<int> vec(tmp);	//Declare and initialize vec vector with tmp vector

vector<int> tmp(vec.begin(), vec.begin() + 3);	//Initialize tmp with the 0th to 2nd values of vector vec

int arr[5] = {1, 2, 3, 4, 5};	
vector<int> vec(arr, arr + 5);		//Use the elements of the arr array to initialize the vec vector
//And VEC Unified pointer () end.
vector<int> vec(&arr[1], &arr[4]); //Take the elements in the range of arr[1]~arr[4] as the initial value of vec

vector<int> v4{10, 1};  // v4 has two elements with values of 10 and 1

vector<string> v5{"hi"}; // List initialization: v5 has an element


vector<object_ type>::iterator it; Normal iterator

  • Start pointer: VEC begin();
  • End pointer: VEC end(); / / point to the next position of the last element
  • Pointer to start of constant: VEC cbegin(); // It means that the content can not be modified through this pointer, but it can be modified in other ways, and the pointer can also be moved.
  • Pointer to the end of the constant: VEC cend()

vector::reverse_iterator ritr; Reverse iterator

  • vec.rbegin(); Points to the last element of the vector
  • vec.rend(); Points to the previous position of the first element of the vector

vector<char>::const_ iterator citr; Constant iterator

  • vec.cbegin();                        
  • vec.cend();                            

cbegin()/cend() determines that the type of iterator returned is const. At this time, even if the type of vector is not const, misoperation of the data can be prevented.


at(idx)Return the data indicated by the index idx. If IDX is out of bounds, throw out_of_range.
back()Returns the last element without checking whether the data exists.
front()Returns the first element.
swap()Swap two vectors.   v1.swap(v2) ; Swap V1, V2
push_back(value)Add an element at the end of the Vector.
pop_back()It removes the last element from the vector.
empty()Judge whether the Vector is null (null when true is returned)
insert(iter,val)It inserts the new element at the specified location.
erase(iterator)Deletes the element of the specified or specified range.
resize(nsize)It modifies the size of the vector.
clear()It removes all elements from the vector.
size()Returns the size of the number of Vector elements.
capacity()Returns the number of elements that the vector can hold (without reallocating memory)
assign()It assigns the new value to the vector.
operator=(vector)It assigns the new value to the vector container.
operator[](pos)It accesses the specified element.
end()Returns the iterator of the last element (the real point to the next position of the last element)
emplace(pos,args)It will insert a new element before the position pos.
emplace_back(val)It inserts a new element at the end.
rend()It points to the element before the first element of the vector.
rbegin()It points to the last element of the vector.
begin()Returns the iterator of the first element.
max_size()Returns the maximum number (upper limit) of elements that a Vector can hold.
cend()It points to the last last element in the quantity.
cbegin()It refers to the first element of the vector.
crbegin()It refers to the last character of the vector.
crend()It refers to the element before the first element of the vector.
data()It writes the data of the vector to the array.
reverse()Element flip
find ()Find element and return address

Traverse vector

vector<int> vec;

//Iterator traversal
vector<int>::iterator it;
for (it = vec.begin(); it != vec.end(); it++)
    cout << *it << endl;

for(auto i:vec)

for (size_t i = 0; i < vec.size(); i++) {
	cout << vec.at(i) << endl;

//at() function
int count=vec.size();
for(int i=0;i<count;i++)

int* p = vec.data();
//vector is a continuously stored array in memory, so you can return a pointer to this array. This is a feature of C++11.

Insert element

//push_back() insert
for (int i = 0; i != 100; i++) {

//Incorrect assignment
for(int i=0;i<10;++i)         //Subscripts can only be used to get elements that already exist

//insert() insert
vec.insert(vec.begin(),6);   //Insert 6 before subscript 0 element
vec.insert(vec.begin()+3,1);  //Insert 1 before subscript 3 element
v.insert(v.end(),3,0);     //Insert 3 zeros in the tail
vec.insert(vec.begin()+1,a+2,a+6);//Insert the third element to the fifth element of a before the subscript 1 element (excluding a+6)


//Sort from small to large
sort(vec.begin(), vec.end());

//Sort from large to small
//1.reverse function
//2. Rewrite cmp
bool cmp(const int& a,const int& b)
    return a>b;

Find element

vector<int>::iterator it; 
it=find(vec.begin(), vec.end(), 6);
    cout<<"can not find"<<endl;

//vector itself has no find method, and its find is realized by algorithm.

Two dimensional array


vector<vector<int> > A;	//The correct way of definition is to add a space before the angle brackets
vector<vector<int>> A;	//This definition is wrong before c++11, and it is supported after c++11

vector<vector<int> >A(3);    //Define the size of two-dimensional dynamic array by 3 lines

vec.size();     //Number of rows of two-dimensional array
vec[0].size();  //Number of columns in a two-dimensional array

Insert operation

vector<vector<int> > A;  // Large container
//A.push_ vector must be in the back
vector<int> B;  // Small container
A.push_back(B); // Put the small container into the large container
B.clear();   // Empty small container element

vector<vector<int> > v;
vector<int> temp;
// Input element  
	for(i=0; i<n; i++) {
		temp.clear(); // Empty the elements in temp; 
		for(j=0; j<n; j++) {
			cin >>  num;

//Traverse all output elements
	for(i=0; i<n; i++) {
		for(j=0; j<n; j++) {
			cout<<v[i][j] << "  ";
		cout <<endl;


Keywords: C++ Algorithm data structure pointer

Added by bluntster on Wed, 09 Feb 2022 01:16:10 +0200