![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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_front( ) and deque emplace_back( ) in C++ in STL
Given is the task to show the functionality of deque emplace_front( ) and deque emplace_back( ) 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 allows users 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 the emplace_front( ) function
The emplace_front( ) function inserts the new element at the beginning of deque.
Syntax
dequename.emplace_front(value)
Parameters
value − It defines the new element to be inserted at the beginning in the deque
Example
Input Deque − 12 13 14 15 16
Output New Deque − 11 12 13 14 15 16
Input Deque − O R C E
Output New Deque: F O R C E
Approach can be followed
First we declare the deque.
Then we print the deque
Then we define the emplace_front( ) function
Then we print the new deque after inserting a new element.
By using the above approach we can enter new elements at the beginning. While defining function we define the new element as parameter. New elements should have the same data type as deque.
Example
// C++ code to demonstrate the working of deque emplace_front( ) 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 << “ “; // defining the emplace_front( ) function deque.emplace_front(78); // 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: 78 85 87 88 89 90 Input – Deque: O I S E Output – New Deque: N O I S E