C++ List Library - emplace_back() Function



Description

The C++ function std::list::emplace_back() inserts new element at the end of list and increases size of list by one.

Declaration

Following is the declaration for std::list::emplace_back() function form std::list 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::list::emplace_back() function.

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l = {1, 2, 3, 4};

   l.emplace_back(5);

   cout << "List contains following element" << endl;

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

   return 0;
}

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

List contains following element
1
2
3
4
5
list.htm
Advertisements