How to Add Elements in a Vector in C++?
Last Updated :
26 Nov, 2024
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 add an element to a vector is by appending it to the end using vector push_back() method. Let's take a look at a simple example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 4, 2};
// Pushing two elements at the end
v.push_back(3);
v.push_back(5);
for (int i : v)
cout << i << " ";
return 0;
}
Though vector push_back() is very easy to use, it can only add elements at the end of the vector.
So, C++ also provides more methods to insert the element. Each of these methods have some special functionality that makes them particularly useful in some cases:
Using vector insert()
The vector insert() method is more versatile than vector push_back() and is able to insert the elments at the any given position. It can also insert multiple elements at once.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 3, 5};
// Inserting one element at index 2
v.insert(v.begin() + 2, 9);
// Inserting two elements at index 1
v.insert(v.begin() + 1, {4, 2});
for (int i : v)
cout << i << " ";
return 0;
}
Inserting elements in the middle is an expensive operation in vector, so if you need frequent insertion and deletion in the middle, prefer other data containers.
Using vector emplace()
The vector emplace() function is also used to insert the element in the vector just like vector insert() but this function construct the element to be inserted directly in the vector. This avoids creating the unnecessary copies of the element. This process is also called emplacing instead of inserting.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 4, 3, 5};
// Emplacing one element at index 2
v.emplace(v.begin() + 2, 2);
for (int i : v)
cout << i << " ";
return 0;
}
More details are discussed in the article - Vector emplace() vs insert() in C++
Using vector emplace_back()
The vector emplace_back() method is the emplace counterpart of the vector push_back() method. It constructs the new element to be inserted directly inside the vector avoiding any extra copies of the elements.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 4, 2};
// Emplacing two elements at the end
v.emplace_back(3);
v.emplace_back(5);
for (int i : v)
cout << i << " ";
return 0;
}
Similar Reads
How to Change an Element in a Vector in C++? In C++, vectors are dynamic containers that store the data in a contiguous memory location but unlike arrays, they can resize themselves to store more elements. In this article, we will learn how to change a specific element by its index in a vector in C++.The simplest way to modify an element is by
3 min read
How to Insert Elements into 2D Vector in C++? In C++, 2D vectors provide several built-in methods to insert elements. The efficiency of the insertion depends on where the insertion occurs. In this article, we will explore different ways to insert elements into a 2D vector in C++ and their specific use cases.The simplest way to add elements to a
3 min read
How to Add Element to Vector of Pairs in C++? In C++, vectors are dynamic arrays that can grow and shrink in size whereas a pair is a container that can store two data elements or objects. A vector of pairs, therefore, is a dynamic array of pairs. In this article, we will learn how to add an element to a vector of pairs in C++. Example: Input:v
2 min read
How to Add an Element to a Set in C++? In C++ STL, a set is a container that stores unique elements in a sorted order. In this article, we will learn how to add an element to a set in C++ STL. Example: Input: mySet = {1, 2, 4, 5, 8} Element to add: 3 Output: mySet = {1, 2, 3, 4, 5, 8}Add an Element to a Set in C++To add a specific elemen
2 min read
How to Add Element at the End of a Vector in C++? 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/st
4 min read