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:
- It has a strong exception guarantee, therefore, no changes are made if an exception is thrown.
- The parameter should be of the same type as that of the container, otherwise, an error is thrown.
Example 1:
#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);
for ( auto it = myvector.begin(); it != myvector.end(); ++it)
cout << ' ' << *it;
return 0;
}
|
Output:
1 2 3 4 5 6
Example 2:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> myvector;
myvector.emplace_back( "This" );
myvector.emplace_back( "is" );
myvector.emplace_back( "a" );
myvector.emplace_back( "computer science" );
myvector.emplace_back( "portal" );
for ( auto it = myvector.begin(); it != myvector.end(); ++it)
cout << ' ' << *it;
return 0;
}
|
Output:
This is a computer science portal
Example 3:
#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' );
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
- Add elements to the vector using emplace_back function
- Check if the size of the vector is 0, if not, increment the counter variable initialised as 0, and pop the back element.
- Repeat this step until the size of the vector becomes 0.
- Print the final value of the variable.
#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()
- 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.
- 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.
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<pair< char , int >> vect;
vect.emplace_back( 'a' , 24);
vect.push_back(make_pair( 'b' , 25));
for ( int i=0; i<vect.size(); i++)
cout << vect[i].first << " " << vect[i].second
<< endl;
return 0;
}
|
Output:
a 24
b 25
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!