Open In App

push_back() vs emplace_back() in C++ STL Vectors

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.

push_back():

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++ program to demonstrate the
// push_back() method
 
#include <iostream>
#include <vector>
 
using namespace std;
 
// Class
class GFG {
 
public:
    int x;
 
    // Parameterized Constructor
    GFG(int x)
        : x(x)
    {
        cout << "Constructor=>"
             << "x:" << x << endl;
    }
 
    // Copy Constructor
    GFG(const GFG& GFG)
        : x(GFG.x)
    {
        cout << "Copied=>"
             << "x:" << x << endl;
    }
 
    ~GFG()
    {
        cout << "destructor=>"
             << "x:" << x << endl;
    }
};
 
// Driver Code
int main()
{
    // Vector of object of GFG class
    // is created
    vector<GFG> vertices;
    cout << "length of vertices:" << vertices.size()
         << endl;
 
    // Inserting elements in the object
    // created using push_back() method
    cout << endl;
    // Custom input entries
    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:

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++ Program to optimize the usage
// of C++ Vectors to reduce the cost
// of copying elements with the
// reserve() method
 
#include <iostream>
#include <vector>
using namespace std;
 
// Class
class GFG {
 
public:
    float x, y;
 
    // Parameterized Constructor
    GFG(float x, float y)
        : x(x), y(y)
    {
    }
 
    // Copy Constructor
    GFG(const GFG& GFG)
        : x(GFG.x), y(GFG.y)
    {
        cout << "Copied" << endl;
    }
};
 
// Driver Code
int main()
{
    // Create object of vector in
    // the main() method
    vector<GFG> vertices;
 
    // Reserve three elements using
    // reserve()
    vertices.reserve(3);
 
    // Add elements to the vector
    // object
    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

 

emplace_back():

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++ Program to illustrate the high
// cost of copying the elements in
// vector in STL
 
#include <iostream>
#include <vector>
using namespace std;
 
// Class
class GFG {
 
public:
    float x, y;
 
    // Parameterized Constructor
    GFG(float x, float y)
        : x(x), y(y)
    {
    }
 
    // Copy Constructor
    GFG(const GFG& GFG)
        : x(GFG.x), y(GFG.y)
    {
        cout << "Copied" << endl;
    }
};
 
// Driver Code
int main()
{
    // Create an object of vector
    // class object
    vector<GFG> vertices;
 
    // Reserve the elements in the
    // vector using reserve() method
    vertices.reserve(3);
 
    // Add element to vector object
    // using emplace_back() method
    vertices.emplace_back(1, 2);
    cout << endl;
 
    vertices.emplace_back(4, 5);
    cout << endl;
 
    vertices.emplace_back(7, 8);
    cout << endl;
 
    return 0;
}

Output: 
 

 

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.

Article Tags :