Deque Emplace Function

C++ Deque emplace

C++ Deque emplace function inserts a new element just before the specified position and the size of the container is increased by one.

Syntax

Example

iterator emplace(const_iterator position,value_type val);

Parameter

position : It defines the position before which the new element is to be inserted.

val : New value which is to be inserted.

Return value

It returns an iterator to the newly constructed element.

Example 1

Let's see a simple example

Example

#include <iostream>
#include<deque>
using namespace std;
int main()
{
  deque<int> deq={7,8,4,5};
  deque<int>::iterator itr;
  deq.emplace(deq.begin(),1);
  for(itr=deq.begin();itr!=deq.end();++itr)
  std::cout << *itr <<" ";
    return 0;
}

Output:

Output

1 7 8 4 5

In this example, emplace function inserts a new element i.e 1 in the beginning of the deque.

Example 2

Let's see a simple example

Example

#include <iostream>
#include<deque>
using namespace std;
int main()
{
  deque<char> d={'C','+'};
  deque<char>::iterator itr=d.begin();
  ++itr;
  d.emplace(itr,'+');
  for(itr=d.begin();itr!=d.end();++itr)
  std::cout << *itr;
  return 0;
}

Output:

In this example, emplace function inserts a new element i.e '+' at second position.

Input Required

This code uses input(). Please provide values below: