How to Add Element at the End of a Vector in C++?
Last Updated :
18 Nov, 2024
Vector allows fast insertion and deletion at the end. In this article, we will learn different ways to add an element at the end of the vector.
The easiest way to add an element at the end of vector is by using vector push_back() function. Let’s take a look at a simple example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v;
// Inserting elements at end of vector
v.push_back(1);
v.push_back(7);
v.push_back(9);
for (auto i : v)
cout << i << " ";
return 0;
}
The push_back() function adds an element at the end of vector and increment the size of vector by 1.
There are also some other methods in C++ to add an element at the end of vector. Following are the ways:
Using Vector emplace_back()
The vector emplace_back() method is similar to the vector push_back() method
but it avoids creating temporary copies by constructing the element to be inserted in-plane inside the vector.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v;
// Inserting elements at end of vector
v.emplace_back(1);
v.emplace_back(7);
v.emplace_back(9);
for (auto i : v)
cout << i << " ";
return 0;
}
Using Vector insert()
The vector insert() method can insert an element at any position in the vector including the end.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v;
// Inserting an element at end of vector
v.insert(v.end(), 1);
v.insert(v.end(), 7);
v.insert(v.end(), 9);
for (auto i : v)
cout << i << " ";
return 0;
}
Explanation: The vector insert() function takes the iterator to where the element is to be inserted as first parameter, which in this case is vector end() iterator and value to insert as second parameter.
Using Vector [] Operator
The [] operator in vector is used to access and update the already present elements. So, first create an element at the end of the vector by increase the size of vector using vector resize() function and then assign an element at the end of vector.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 7};
// Increasing the size of vector
v.resize(v.size() + 1);
// Assign the element at end of vector
v.back() = 9;
for (auto i : v)
cout << i << " ";
return 0;
}
Using back_inserter()
The back_intserter() function resize the given array by adding an element with the default value at the and return the iterator to this element. This iterator can be then dereferenced to assign some new value.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v;
// Inserting an element at end of vector
*back_inserter(v) = 1;
*back_inserter(v) = 7;
*back_inserter(v) = 9;
for (auto i : v)
cout << i << " ";
return 0;
}
This method is mostly used with STL algorithms for containers that supports the push_back() operation.
Similar Reads
How to Add Multiple Elements at the End of Vector in C++? In C++, vectors are dynamic arrays that can grow and reduce in size as per requirements. In this article, we will learn how to add multiple elements at the end of a vector in C++. Example Input:myVector = {10, 20, 30, 40, 50}elements_to_add = {60, 70, 80}Output:updated_vector: 10 20 30 40 50 60 70 8
2 min read
How to Add an Element at the End of a Deque in C++? In C++, a deque is a flexible data structure that supports the insertion and deletion of elements from both ends efficiently. In this article, we will learn how to add an element at the end of a deque in C++. Example: Input: myDeque ={10,20,30,40} Output: // added element 50 at the end myDeque ={10,
2 min read
How to Add an Element at the End of List in C++? In C++, the Standard Template Library (STL) has a doubly-linked list container, known as std::list that offers various functions to manipulate elements. In this article, we will learn how to add an element at the end of a list in C++ STL. Example: Input: myList = {1, 2, 3}; Output: List after Elemen
2 min read
How to Access the First Element of a Vector in C++? In this article, we will learn how to access the first element of vector in C++.The most efficient way to access the first element of vector is by using vector front() function. Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<int
2 min read
How to Add Elements in a Vector in C++? In C++, vector provides several built-in methods to insert the elements and efficiency of the insertion depends on the position where the insertion takes place. In this article, we will learn different ways to insert elements into a vector in C++ and also compare their efficiency.The simplest way to
3 min read