public member function
<deque>

std::deque::emplace

template <class... Args>  iterator emplace (const_iterator position, Args&&... args);
Construct and insert element
The container is extended by inserting a new element at position. This new element is constructed in place using args as the arguments for its construction.

This effectively increases the container size by one.

Double-ended queues are designed to be efficient performing insertions (and removals) from either the end or the beginning of the sequence. Insertions on other positions are usually less efficient than in list or forward_list containers. See emplace_front and emplace_back for member functions that extend the container directly at the beginning or at the end.

The element is constructed in-place by calling allocator_traits::construct with args forwarded.

Parameters

position
Position in the container where the new element is inserted.
Member type const_iterator is a random access iterator type that points to a constant element.
args
Arguments forwarded to construct the new element.

Return value

An iterator that points to the newly emplaced element.

Member type iterator is a random access iterator type that points to an element.

The storage for the new element is allocated using allocator_traits<allocator_type>::construct(), which may throw exceptions on failure (for the default allocator, bad_alloc is thrown if the allocation request does not succeed).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// deque::emplace
#include <iostream>
#include <deque>

int main ()
{
  std::deque<int> mydeque = {10,20,30};

  auto it = mydeque.emplace ( mydeque.begin()+1, 100 );
  mydeque.emplace ( it, 200 );
  mydeque.emplace ( mydeque.end(), 300 );

  std::cout << "mydeque contains:";
  for (auto& x: mydeque)
    std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}
Output:
mydeque contains: 10 200 100 20 30 300


Complexity

Depending on the particular library implemention, up to linear in the number of elements between position and one of the ends of the deque.

Iterator validity

If the insertion happens at the beginning or the end of the sequence, all iterators related to this container are invalidated, but pointers and references remain valid, referring to the same elements they were referring to before the call.
If the insertion happens anywhere else in the deque, all iterators, pointers and references related to this container are invalidated.

Data races

The container is modified.
If the insertion happens at the beginning or the end of the sequence, no contained elements are accessed (although see iterator validity above).
If it happens anywhere else, it is not safe to concurrently access elements.

Exception safety

If position is begin or end, there are no changes in the container in case of exception (strong guarantee).
Otherwise, the container is guaranteed to end in a valid state (basic guarantee).
If allocator_traits::construct is not supported with the appropriate arguments, or if position is not valid, it causes undefined behavior.

See also