C++ Deque::emplace_back() Function
The C++ std::deque::emplace_back() function is used to insert a new element at the end of the deque. Unlike push_back(), which copies or moves an existing object, emplace_back() constructs the element using the provided arguments. It avoids the unnecessary copying or moving of the object.
Syntax
Following is the syntax for std::deque::emplace_back() function.
void emplace_back (Args&&... args);
Parameters
- args − It indicates the arguments forwarded to construct the new element.
Return value
It does not return anything.
Exceptions
If reallocation fails bad_alloc exception is thrown.
Time complexity
The time complexity of this function is Constant i.e. O(1)
Example
In the following example, we are going to use the emplace_back() function on the integers deque.
#include <iostream>
#include <deque>
int main()
{
std::deque<int> a;
a.emplace_back(1);
a.emplace_back(22);
a.emplace_back(333);
for (const auto &elem : a) {
std::cout << elem << " ";
}
return 0;
}
Output
Output of the above code is as follows −
1 22 333
Example
Consider the another scenario, where we are going to apply the emplace_back() function on the strings deque.
#include <deque>
#include <iostream>
#include <string>
int main()
{
std::deque<std::string> a;
a.emplace_back("TP");
a.emplace_back("TutorialsPoint");
for(const std::string& str : a) {
std::cout << str << " ";
}
return 0;
}
Output
Following is the output of the above code −
TP TutorialsPoint