
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
Push Front in C++ STL Deque
In this article we will be discussing the working, syntax and examples of deque::push_front() 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 deque::push_front()?
deque::push_front() is an inbuilt function in C++ STL which is declared in header file. deque::push_front() is used to push/insert an element at the front or at the beginning of the deque container making the pushed/inserted element as the first element of the deque. This function accepts one argument, that is, the element which is to be pushed/inserted at the beginning.
Syntax
mydeque.push_front(const value_type& value); This function accepts one parameter which is the element which is to be inserted
Return value
This function returns nothing.
Example
Input: deque<int> mydeque = {10, 20 30, 40}; mydeque.push_front(9); Output: Deque elements: 9 10 20 30 40 Input: deque<int> mydeque; mydeque.push_front(5); Output: 5
Example
#include <deque> #include <iostream> using namespace std; int main(){ deque<int> Deque = { 20, 30, 40, 50 }; Deque.push_front(10); cout<<"Elements in Deque are : "; for(auto i = Deque.begin(); i!= Deque.end(); ++i) cout << ' ' << *i; }
Output
If we run the above code it will generate the following output −
Elements in Deque are : 10 20 30 40 50
Example
#include <deque> #include <iostream> using namespace std; int main(){ int total = 0; deque<int> Deque; Deque.push_front(10); Deque.push_front(20); Deque.push_front(30); Deque.push_front(40); while (!Deque.empty()){ total++; Deque.pop_front(); } cout<<"Total number of elements in a deque are : "<<total; return 0; }
Output
If we run the above code it will generate the following output −
Total number of elements in a deque are : 4