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.
SyntaxFollowing is the syntax for std::deque::emplace_back() function.
void emplace_back (Args&&... args);Parameters
It does not return anything.
ExceptionsIf reallocation fails bad_alloc exception is thrown.
Time complexityThe time complexity of this function is Constant i.e. O(1)
ExampleIn 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 333Example
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
deque.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4