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
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
#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:
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
#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.