A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_deque_emplace_back.htm below:

C++ Deque::emplace_back() Function

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 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

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