C++ Deque Library - emplace_back() Function


Advertisements

Description

The C++ function std::deque::emplace_back() inserts new element at the end of deque and increases size of deque by one. If reallocation happens storage requirement for this container is fulfilled by internal allocator.

Declaration

Following is the declaration for std::deque::emplace_back() function form std::deque header.

C++11

template <class... Args>
void emplace_back (Args&&... args);

Parameters

args − Arguments forwarded to construct the new element.

Return value

None.

Exceptions

If reallocation fails bad_alloc exception is thrown.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::deque::emplace_back() function.

#include <iostream>
#include <deque>

using namespace std;

int main(void) {

   deque<int> d = {1, 2, 3};

   d.emplace_back(4);
   d.emplace_back(5);

   cout << "Contents of deque are" << endl;

   for (auto it = d.begin(); it != d.end(); ++it)
      cout << *it << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Contents of deque are
1
2
3
4
5
deque.htm
Advertisements