Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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:

  • 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++




// 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++




// 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.


Last Updated : 03 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads