The Wayback Machine - https://web.archive.org/web/20240527123818/https://www.geeksforgeeks.org/vectoremplace_back-c-stl/
Open In App

vector::emplace_back in C++ STL

Last Updated : 06 Feb, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.

vector::emplace_back()

This function is used to insert a new element into the vector container, the new element is added to the end of the vector.
Syntax :

vectorname.emplace_back(value)
Parameters :
The element to be inserted into the vector
is passed as the parameter.
Result :
The parameter is added to the
vector at the end position.

Examples:

Input: myvector{1, 2, 3, 4, 5};
         myvector.emplace_back(6);
Output: myvector = 1, 2, 3, 4, 5, 6

Input: myvector{};
         myvector.emplace_back(4);
Output: myvector = 4

Errors and Exceptions:

  1. It has a strong exception guarantee, therefore, no changes are made if an exception is thrown.
  2. The parameter should be of the same type as that of the container, otherwise, an error is thrown.

Example 1:




// INTEGER VECTOR EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include <iostream>
#include <vector>
using namespace std;
  
int main()
{
    vector<int> myvector;
    myvector.emplace_back(1);
    myvector.emplace_back(2);
    myvector.emplace_back(3);
    myvector.emplace_back(4);
    myvector.emplace_back(5);
    myvector.emplace_back(6);
    // vector becomes 1, 2, 3, 4, 5, 6
  
    // printing the vector
    for (auto it = myvector.begin(); it != myvector.end(); ++it)
        cout << ' ' << *it;
   
    return 0;
      
}


Output:

1 2 3 4 5 6

Example 2:




// STRING VECTOR EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include <iostream>
#include <vector>
#include <string>
using namespace std;
  
int main()
{
    // vector declaration
    vector<string> myvector;
    myvector.emplace_back("This");
    myvector.emplace_back("is");
    myvector.emplace_back("a");
    myvector.emplace_back("computer science");
    myvector.emplace_back("portal");
  
    // vector becomes This, is, a computer science, portal
  
    // printing the vector
    for (auto it = myvector.begin(); it != myvector.end(); ++it)
        cout << ' ' << *it;
   
    return 0;
      
}


Output:

This is a computer science portal

Example 3:




// CHARACTER VECTOR EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include <iostream>
#include <vector>
using namespace std;
  
int main()
{
    vector<char> myvector;
    myvector.emplace_back('a');
    myvector.emplace_back('c');
    myvector.emplace_back('x');
    myvector.emplace_back('y');
    myvector.emplace_back('z');
    // vector becomes a, c, x, y, z
  
    // printing the vector
    for (auto it = myvector.begin(); it != myvector.end(); ++it)
        cout << ' ' << *it;
   
    return 0;
      
}


Output:

a, c, x, y, z

Time Complexity: O(1)

Application:
Given an empty vector, add integers to it using emplace_back function and then calculate its size.

Input  : 1, 2, 3, 4, 5, 6
Output : 6

Algorithm

  1. Add elements to the vector using emplace_back function
  2. Check if the size of the vector is 0, if not, increment the counter variable initialised as 0, and pop the back element.
  3. Repeat this step until the size of the vector becomes 0.
  4. Print the final value of the variable.




// CPP program to illustrate
// Application of emplace_back function
#include <iostream>
#include <vector>
using namespace std;
   
int main()
{
    int count = 0;
    vector<int> myvector;
    myvector.emplace_back(1);
    myvector.emplace_back(2);
    myvector.emplace_back(3);
    myvector.emplace_back(4);
    myvector.emplace_back(5);
    myvector.emplace_back(6);
    while (!myvector.empty()) {
        count++;
        myvector.pop_back();
    }
    cout << count;
    return 0;
}


Output:

6

emplace_back() vs push_back()

  1. push_back() copies a string into a vector. First, a new string object will be implicitly created initialized with provided char*. Then push_back will be called which will copy this string into the vector using the move constructor because the original string is a temporary object. Then the temporary object will be destroyed.
  2. emplace_back() constructs a string in-place, so no temporary string will be created but rather emplace_back() will be called directly with char* argument. It will then create a string to be stored in the vector initialized with this char*. So, in this case, we avoid constructing and destroying an unnecessary temporary string object.

Please see emplace vs insert in C++ STL for details.




// C++ code to demonstrate difference between
// emplace_back and insert_back
#include<bits/stdc++.h>
using namespace std;
    
int main()
{
    // declaring priority queue
    vector<pair<char, int>> vect;
        
    // using emplace() to insert pair in-place
    vect.emplace_back('a', 24);
        
    // Below line would not compile
    // vect.push_back('b', 25);    
        
    // using push_back() to insert
    vect.push_back(make_pair('b', 25));    
        
    // printing the vector
    for (int i=0; i<vect.size(); i++)
        cout << vect[i].first << " " << vect[i].second
             << endl;
   
    return 0;
}


Output:

a 24
b 25


Similar Reads

list::emplace_front() and list::emplace_back() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::emplace_front()This function is used to insert a new element into the list container, the new
3 min read
deque::emplace_front() and deque::emplace_back() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::emplace_front()This fun
5 min read
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
5 min read
vector::front() and vector::back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::front() This function can be used to fetch the first element of a vector container.Syntax : vectorname.front() Parameters : No value is needed to pass as
3 min read
vector::empty() and vector::size() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::empty() The empty() function is used to check if the vector container is empty or not.Syntax : vectorname.empty() Parameters : No parameters are p
5 min read
vector::push_back() and vector::pop_back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::push_back() push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the cur
4 min read
vector::operator= and vector::operator[ ] in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::operator= This operator is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new
4 min read
vector::at() and vector::swap() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::at() at() function is used reference the element present at the position given as the parameter to the function. Syntax: vectorname.at(position) Parameter
4 min read
vector::begin() and vector::end() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::begin() begin() function is used to return an iterator pointing to the first element of the vector container. begin() function returns a bidirectional
3 min read
vector :: cbegin() and vector :: cend() in C++ STL
Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container. vector::cbegin() The function returns an iterator which is used to iterate container. The iterator points to the beginning of the vector.Iterator cannot modify the contents of the vector. Synta
2 min read
Article Tags :
Practice Tags :