In C++, vectors are dynamic arrays, that can grow or shrink and their storage is handled by the container itself. There are two ways of inserting an element in a vector. They are push_back() and emplace_back(). In this article, we will discuss the difference between them.
This method is used to insert elements in a vector from the end of the container. As the flexibility in the size of the vector is dynamic, the size of the container also increased by 1 after inserting any new element.
Program 1:
C++
#include <iostream>
#include <vector>
using namespace std;
class GFG {
public :
int x;
GFG( int x)
: x(x)
{
cout << "Constructor=>"
<< "x:" << x << endl;
}
GFG( const GFG& GFG)
: x(GFG.x)
{
cout << "Copied=>"
<< "x:" << x << endl;
}
~GFG()
{
cout << "destructor=>"
<< "x:" << x << endl;
}
};
int main()
{
vector<GFG> vertices;
cout << "length of vertices:" << vertices.size()
<< endl;
cout << endl;
vertices.push_back(GFG(1));
cout << endl;
vertices.push_back(GFG(11));
cout << endl;
vertices.push_back(GFG(21));
cout << endl;
return 0;
}
|
Output
length of vertices:0
Constructor=>x:1
Copied=>x:1
destructor=>x:1
Constructor=>x:11
Copied=>x:11
Copied=>x:1
destructor=>x:1
destructor=>x:11
Constructor=>x:21
Copied=>x:21
Copied=>x:1
Copied=>x:11
destructor=>x:1
destructor=>x:11
destructor=>x:21
destructor=>x:1
destructor=>x:11
destructor=>x:21
Explanation:
- Everytime push_back a value to a vector , cpp will create a new vector which copied the new value from parameter and all values in the old vector
- In the main function, a vector(vector1) of class GFG is created of size is 0. When using GFG(int) as parameter, it is created using a parameterized constructor, and since the vector is of type GFG, the object is passed to the “GFG” copy constructor, hence “Copied” is printed once.
- When the second GFG value has been pushed_back, a new vector(vector2) will be created with greater size and Copied the GFG(11) into it. And then all the values in the old vector which refers to the vector1{1} Copied to the Vector2, so the “Copied” is printed twice when the second GFG has been pushed_back.
- Similarly, when the third object is to be created, a new vector of greater size is allocated in memory, the first element is copied, then the second element is copied, and then the third element is copied. Hence, “Copied” is printed thrice as the copy constructor is called thrice, once for each element inserted.
- However, if the size of the vector is fixed, then before inserting any element, the number of times “Copied” will be printed is one. This is because as the required size is declared, no re-copying of elements takes place upon each insertion.
Note: reserve() is used instead of using “vector<Point> vertices(3)”, as the below syntax sometimes doesn’t work because there is no default constructor defined in the class.
"vector<GFG> vertices(3);”
Program 2:
The cost due to the copying of elements is high. Below is the program to optimize the usage of C++ Vectors to reduce the cost of copying elements:
C++
#include <iostream>
#include <vector>
using namespace std;
class GFG {
public :
float x, y;
GFG( float x, float y)
: x(x), y(y)
{
}
GFG( const GFG& GFG)
: x(GFG.x), y(GFG.y)
{
cout << "Copied" << endl;
}
};
int main()
{
vector<GFG> vertices;
vertices.reserve(3);
vertices.push_back(GFG(1, 2));
cout << endl;
vertices.push_back(GFG(4, 5));
cout << endl;
vertices.push_back(GFG(7, 8));
cout << endl;
return 0;
}
|
Output:
Copied
Copied
Copied
This method is used instead of creating the object using parameterized constructor and allocating it into a different memory, then passing it to the copy constructor, which will insert it into the vector. This function can directly insert the object without calling the copy constructor. Below is the program to illustrate the same:
Program 3:
C++
#include <iostream>
#include <vector>
using namespace std;
class GFG {
public :
float x, y;
GFG( float x, float y)
: x(x), y(y)
{
}
GFG( const GFG& GFG)
: x(GFG.x), y(GFG.y)
{
cout << "Copied" << endl;
}
};
int main()
{
vector<GFG> vertices;
vertices.reserve(3);
vertices.emplace_back(1, 2);
cout << endl;
vertices.emplace_back(4, 5);
cout << endl;
vertices.emplace_back(7, 8);
cout << endl;
return 0;
}
|
Explanation: In the above program nothing is printed as the copying of the constructor doesn’t take place while using the emplace_back() function.
Let us see the difference between in a tabular form -:
|
Push_back |
emplace_back |
1. |
It is used to insert the element in a vector or a string |
It is used to insert an element in a vector or a string. |
2. |
It is slower. |
It is faster. |
3. |
Its syntax is :
push_back(value_to_insert)
|
Its syntax is -:
emplace_back(value_to_insert)
|
4. |
push_back accepts the only object of the type if the constructor accept more than one arguments |
emplace_back accept arguments of the constructor of the type. |
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!