Skip to content
Related Articles

Related Articles

Improve Article
push_back() vs emplace_back() in C++ STL Vectors
  • Difficulty Level : Easy
  • Last Updated : 02 Jun, 2021

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:
    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()
{
    // Vector of object of GFG class
    // is created
    vector<GFG> vertices;
 
    // Inserting elements in the object
    // created using push_back() method
 
    // Custom input entries
    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

Copied
Copied
Copied

 

Explanation:



  • In the main function, a vector of class GFG is created of size is 1. Before inserting the first GFG object into the array, 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. But when another element is to be inserted, a new vector of size greater than the previous one is created, then the “first” element, is again copied, and then the second object is created and copied, hence “Copied” is printed twice, upon execution.
  • 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. 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :