
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Deque Emplace in C++ STL
Given is the task to show the functionality of Deque emplace( ) function in C++ STL
What is Deque?
Deque is the Double Ended Queues that are the sequence containers which provides the functionality of expansion and contraction on both the ends. A queue data structure allow user to insert data only at the END and delete data from the FRONT. Let’s take the analogy of queues at bus stops where the person can be inserted to a queue from the END only and the person standing in the FRONT is the first to be removed whereas in Double ended queue the insertion and deletion of data is possible at both the ends.
What is emplace( ) function?
The emplace( ) function inserts the new element before specified position in deque, and increase the deque as size of element.
Syntax
iterator emplace(const_iterator position, value_type value);
Parameters
Position − It defines the position in the container where the new element is inserted.
Value − It also defines the new value or argument which is to be inserted in container.
Return Value − It returns an iterator which points to the newly inserted element in the deque.
Example
Input Deque − 96 97 98 100
Output New Deque after inserting new element − 96 97 98 99 100
Input Deque − C P T A I N
Output New Deque after inserting new element − C A P T A I N
Approach can be followed
First we declare the deque.
Then we print the deque.
Then we define the emplace( ) function.
Then we print the new deque after inserting new element.
By using above approach we can enter new element in deque. While defining the emplace( ) function we also define the position and we also define the new value which to be inserted in the deque.
Example
// C++ code to demonstrate the working of deque emplace( ) function #include<iostream.h> #include<deque.h> Using namespace std; int main ( ){ // initializing the deque Deque<int> deque = { 85, 87, 88, 89, 90 }; // print the deque cout<< “ Deque: “; for( auto x = deque.begin( ); x != deque.end( ); ++x) cout<< *x << “ “; deque<int> iterator x; // defining the emplace( ) function deque.emplace(deque.emplace( ) + 1, 85); // printing deque after inserting new element cout<< “ New Deque:”; for( x = deque.begin( ) ; x != deque.end( ); ++x) cout<< “ “ <<*x; return 0; }
Output
If we run the above code then it will generate the following output
Input - Deque: 85 87 88 89 90 Output - New Deque: 85 86 87 88 89 90
Example
// C++ code to demonstrate the working of deque emplace( ) function #include<iostream.h> #include<deque.h> Using namespace std; int main( ){ // initializing deque deque<char> deque ={ ‘L’ , ‘A’ , ‘C’ , ‘K’ }; cout<< “ Deque: “; for( auto x = deque.begin( ); x != deque.end( ); ++x) cout<< *x << “ “; deque<int> iterator x; // defining the emplace( ) function deque.emplace(deque.emplace( ) , ‘B’) // printing deque in after inserting new element cout<< “ New deque:”; for( auto x = deque.begin( ) ; x >= deque.end( ); ++x) cout<< “ “ <<*x; return 0; }
Output
If we run the above code then it will generate the following output
Input – Deque: L A C K Output – New Deque : B L A C K