Last Updated on 2021-11-02 by Clay
vector is a sequence container of C++ Standard Template Library (STL). It is similar with Array, but it can automatically expand the space when we store more elements, which is convenient.
This note records several common methods of using the vector container:
begin()
The begin()
function returns an iterator to the beginning of the vector, which can be used to traverse the entire vector. I will introduce how to do that with end()
function.
#include <iostream> #include <vector> using namespace std; int main() { // Init vector<int> v; for (int i=0; i<10; i++) { v.push_back(i*10); } // begin vector<int>::iterator it = v.begin(); cout << *it << endl; return 0; }
Output:
0
It can be confirmed by printing the value of the first element in the vector, the iterator returned by begin()
does point to the beginning of the vector.
end()
In contrast to begin()
, end()
function points to the end of vector (not the last element). It can be used as the end point of the loop in the traversal.
#include <iostream> #include <vector> using namespace std; int main() { // Init vector<int> v; for (int i=0; i<10; i++) { v.push_back(i*10); } // begin vector<int>::iterator it = v.begin(); // while while (it != v.end()) { cout << *it << endl; it++; } return 0; }
Output:
0
10
20
30
40
50
60
70
80
90
size()
size()
, as the name implies, is used to take the size of the vector.
#include <iostream> #include <vector> using namespace std; int main() { // Init vector<int> v; for (int i=0; i<10; i++) { v.push_back(i*10); } // size cout << "vector size: " << v.size() << endl; return 0; }
Output:
vector size: 10
capacity()
The value obtained by capacity()
and size()
is different. size()
is the number of elements in the current vector, capacity()
is the full space capacity of this vector.
The following example code is used to view the changes of dynamically added elements.
#include <iostream> #include <vector> using namespace std; int main() { // Init vector<int> v; for (int i=0; i<20; i++) { v.push_back(i*10); printf("v[%d]=%d, size=%lu, capacity=%lu\n", i, v[i], v.size(), v.capacity()); } return 0; }
Output:
v[0]=0, size=1, capacity=1
v[1]=10, size=2, capacity=2
v[2]=20, size=3, capacity=4
v[3]=30, size=4, capacity=4
v[4]=40, size=5, capacity=8
v[5]=50, size=6, capacity=8
v[6]=60, size=7, capacity=8
v[7]=70, size=8, capacity=8
v[8]=80, size=9, capacity=16
v[9]=90, size=10, capacity=16
v[10]=100, size=11, capacity=16
v[11]=110, size=12, capacity=16
v[12]=120, size=13, capacity=16
v[13]=130, size=14, capacity=16
v[14]=140, size=15, capacity=16
v[15]=150, size=16, capacity=16
v[16]=160, size=17, capacity=32
v[17]=170, size=18, capacity=32
v[18]=180, size=19, capacity=32
v[19]=190, size=20, capacity=32
As you can see, the vector increases the space dynamically. Every time the vector is stored, it will automatically expand the available space for us, which is convenient.
empty()
empty()
is very simple, it checks whether the vector is empty.
#include <iostream> #include <vector> using namespace std; int main() { // Init vector<int> v; cout << "Before push_back(): " << v.empty() << endl; for (int i=0; i<10; i++) { v.push_back(i*10); } cout << "After push_back(): " << v.empty() << endl; return 0; }
Output:
Before push_back(): 1
After push_back(): 0
front()
Returns the first element of the vector.
#include <iostream> #include <vector> using namespace std; int main() { // Init vector<int> v; for (int i=0; i<10; i++) { v.push_back(i*10); } // front() cout << "front(): " << v.front() << endl; return 0; }
Output:
front(): 0
back()
Returns the last element of the vector.
#include <iostream> #include <vector> using namespace std; int main() { // Init vector<int> v; for (int i=0; i<10; i++) { v.push_back(i*10); } // back() cout << "back(): " << v.back() << endl; return 0; }
Output:
back(): 90
push_back()
Simply put, push_back()
allows us to add new values to the ned of the vector.
#include <iostream> #include <vector> using namespace std; int main() { // Init vector<int> v; for (int i=0; i<3; i++) { v.push_back(i*10); cout << v[i] << endl; } // Divider cout << "============================" << endl; // push_back() v.push_back(1000); for (auto n: v) { cout << n << endl; } return 0; }
Output:
0
10
20
============================
0
10
20
1000
pop_back()
Contrary to push_back()
, pop_back()
deletes the last item.
#include <iostream> #include <vector> using namespace std; int main() { // Init vector<int> v; for (int i=0; i<3; i++) { v.push_back(i*10); cout << v[i] << endl; } // Divider cout << "============================" << endl; // pop_back() v.pop_back(); for (auto n: v) { cout << n << endl; } return 0; }
Output:
0
10
20
============================
0
10
insert()
if you want to insert a new element in the vector, you can use insert()
to handle it. The insert()
function is used as follows:
v.insert(position, n, val)
- position: The index value of the inserted element
- n: Number of element insertions
- val: Inserted element value
Let's take a simple example:
#include <iostream> #include <vector> using namespace std; int main() { // Init vector<int> v; for (int i=0; i<10; i++) { v.push_back(i*10); cout << v[i] << endl; } // Divider cout << "============================" << endl; // insert() v.insert(v.begin()+3, 5, 100); // Print for (auto n: v) { cout << n << endl; } return 0; }
Output:
0
10
20
30
40
50
60
70
80
90
============================
0
10
20
100
100
100
100
100
30
40
50
60
70
80
90
In can be found that after inserting elements, there are several more elements in the vector by 100. This is the effect we want.
erase()
Unlike insert()
, erase()
deletes an element. You need to use an iterator to specify the deleted element or position, and it will also return an iterator pointing to the next element of the deleted element.
#include <iostream> #include <vector> using namespace std; int main() { // Init vector<int> v; for (int i=0; i<10; i++) { v.push_back(i*10); cout << v[i] << " "; } cout << endl; // erase() vector<int>::iterator it; for (it=v.begin(); it!=v.end(); it++) { if (*it == 20) { it = v.erase(it); } } // Print for (auto n: v) { cout << n << " "; } cout << endl; return 0; }
Output:
0 10 20 30 40 50 60 70 80 90
0 10 30 40 50 60 70 80 90
It can be found that we did delete the 20 element.
clear()
clear()
can be said to be the simplest function, the function is clear the entire vector.
#include <iostream> #include <vector> using namespace std; int main() { // Init vector<int> v; for (int i=0; i<10; i++) { v.push_back(i*10); cout << v[i] << " "; } cout << endl; // clear() v.clear(); // Print for (auto n: v) { cout << n << " "; } return 0; }
Output:
0 10 20 30 40 50 60 70 80 90
It can be seen that we only printed the part of the element that was originally put into the vector. After clearing the vector with clear()
, we can not print any elements, because the vector is already empty.
References
- https://www.tutorialspoint.com/cplusplus/cpp_stl_tutorial.htm
- https://www.geeksforgeeks.org/vector-in-cpp-stl/
- https://www.bitdegree.org/learn/c-plus-plus-vector