Deque Emplace Function - C++ Programming Tutorial
C++ Course / STL Queue & Stack / Deque Emplace Function

Deque Emplace Function

BLUF: Mastering Deque Emplace Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Deque Emplace Function

C++ is renowned for its efficiency. Learn how Deque Emplace Function enables low-level control and high-performance computing in the tutorial below.

C++ Deque emplace

The emplace function in C++ for Deque adds a fresh element immediately prior to the indicated position, thereby expanding the container's size by one.

Syntax

Example

iterator emplace(const_iterator position,value_type val);

Parameter

It specifies the placement where the new element will be added before.

val : New value which is to be inserted.

Return value

It provides an iterator to the recently created 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 instance, the emplace method adds a new element, specifically 1, to the start 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 instance, the emplace method adds a new element, represented by '+', at the second position.

Input Required

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

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience