Hi programmer’s, this is gonna be very short and simple explanation. In this article, I’m gonna explain you differences between push_back
and emplace_back
methods used inside vector container.
Difference between emplace_back and push_back method in vector
If you’re using c++ for a while, then you might have seen people using emplace_back
to insert element at last position of the vector. Have you ever wondered?, why they didn’t use push_back
. That’s what we are gonna discuss today in this blog. But first of let’s see how both of these work actually.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec {};
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
vec.push_back(6); // {3, 4, 5, 6}
cout << vec.at(vec.size() - 1); // 6
vec.emplace_back(10); // {3, 4, 5, 6, 10}
cout << vec.at(vec.size() - 1); // 10
}
In given program, you can clearly see that we used both emplace_back
and push_back
, and both these basically insert the element at last index, then what is the difference?
push_back
: push_back is vector method used to insert item at last position, but it takes an element as an argument and then copies it inside vecor at last position.emplace_back
: emplace_back is also works the same as push_back, but the only difference is that it takes constructor values as an argument and then creates new item inside vector, and inserts it at last index.
I know some of you are still confused, but let me explain this by code example. which will make it clear to you.
#include <iostream>
#include <vector>
using namespace std;
struct point {
int x;
int y;
point(int x, int y) {
this->x = x;
this->y = y;
}
};
int main() {
vector<point> vec;
point p1 = point(3,34);
vec.push_back(p1);
cout << "X: " << vec.at(vec.size() - 1).x << " Y: " << vec.at(vec.size() - 1).y << endl; // X : 3 Y : 34
vec.emplace_back(55,32);
cout << "X: " << vec.at(vec.size() - 1).x << " Y: " << vec.at(vec.size() - 1).y << endl; // X : 55 Y: 32
}
In above code, I’ve defined one struct
called point
. and in main function we defined one vector
of point
struct. which means a vector that can only hold point type data.
Push Back: when I used push_back
method then you can clearly see that first I created an instance called p1
. then passed this inside push_back(p1
) method. Now here, this method took p1 as an argument, so it is going to make a copy of it and then will place this at last index.
Emplace Back: when I was using emplace_back
, then I directly passed the constructor value(x and y) as an arguments. and then it constructs the instance of point
in place at the end of the container.
And because of this, emplace_back
has one advantage over the push_back
method. because unlike a push_back
method, it doesn’t involve copying an element being added. And it creates an element in place at the end of the vector container. that’s why its more efficient as compared push_back
.
You may also like: Bubble Sort algortihm in C++ and Javascript
I hope you get what you’re came for. If yes then please don’t forget to share article with your friends.
Thanks for reading 🙂