Vector
Vector
Vector
In C++, vectors are used to store elements of similar data types. However,
unlike arrays, the size of a vector can grow dynamically.
That is, we can change the size of the vector during the execution of a
program as per our requirements.
Vectors are part of the C++ Standard Template Library. To use vectors, we
need to include the vector header file in our program.
#include <vector>
std::vector<T> vector_name;
The type parameter <T> specifies the type of the vector. It can be any
primitive data type such as int , char , float , etc. For example,
vector<int> num;
Method 1:
// Initializer list
vector<int> vector1 = {1, 2, 3, 4, 5};
// Uniform initialization
vector<int> vector2 {1, 2, 3, 4, 5};
Here, we are initializing the vector by providing values directly to the vector.
Now, both vector1 and vector2 are initialized with values 1, 2, 3, 4, 5.
Method 2:
#include <vector>
int main() {
// initializer list
// uniform initialization
// method 3
// ranged loop
// ranged loop
// ranged loop
for (int i : vector3) {
return 0;
Output
vector1 = 1 2 3 4 5
vector2 = 6 7 8 9 10
vector3 = 12 12 12 12 12
Here, we have declared and initialized three different vectors using three
different initialization methods and displayed their contents.
Access elements
Change elements
Remove elements
1. Add Elements to a Vector
To add a single element into a vector, we use the push_back() function. It
inserts an element into the end of the vector. For example,
#include <iostream>
#include <vector>
int main() {
num.push_back(6);
num.push_back(7);
return 0;
}
Output
Initial Vector: 1 2 3 4 5
Updated Vector: 1 2 3 4 5 6 7
Here, we have initialized an int vector num with the elements {1, 2, 3, 4,
num.push_back(6);
num.push_back(7);
Note: We can also use the insert() and emplace() functions to add
elements to a vector.
#include <vector>
int main() {
return 0;
Output
Element at Index 0: 1
Element at Index 2: 3
Element at Index 4: 5
Here,
// throws an exception
cout << num.at(4);
3. Change Vector Element
We can change an element of the vector using the same at() function. For
example,
#include <iostream>
#include <vector>
int main() {
num.at(1) = 9;
num.at(4) = 7;
return 0;
}
Output
Initial Vector: 1 2 3 4 5
Updated Vector: 1 9 3 4 7
num.at(1) = 9;
num.at(4) = 7;
#include <vector>
int main() {
// initial vector
prime_numbers.pop_back();
// final vector
return 0;
Output
Initial Vector: 2 3 5 7
Updated Vector: 2 3 5
prime_numbers.pop_back();
Here, we have removed the last element (7) from the vector.
Function Description
vector<T>::iterator iteratorName;
For example, if we have 2 vectors of int and double types, then we will
need 2 different iterators corresponding to their types:
We can initialize vector iterators using the begin() and end() functions.
1. begin() function
The begin() function returns an iterator that points to the first element of the
vector. For example,
2. end() function
The end() function points to the theoretical element that comes after the
final element of the vector. For example,
Here, due to the nature of the end() function, we have used the
code num.end() - 1 to point to the last element of the num vector i.e. num[2] .
#include <vector>
int main() {
// declare iterator
vector<int>::iterator iter;
iter = num.begin();
iter = num.begin() + 2;
iter = num.end() - 1;
return 0;
Output
num[0] = 1
num[2] = 3
num[4] = 5
In this program, we have declared an int vector iterator iter to use it with
the vector num .
// declare iterator
vector<int>::iterator iter;
Then, we initialized the iterator to the first element of the vector using
the begin() function.
Then, we printed the 3rd element of the vector by changing the value
of iter to num.begin() + 2 .
Finally, we printed the last element of the vector using the end() function.
#include <vector>
int main() {
vector<int>::iterator iter;
return 0;
Output
1 2 3 4 5
Here, we have used a for loop to initialize and iterate the iterator iter from
the beginning of the vector to the end of the vector using
the begin() and end() functions.