In the C++ programming language, the deque (double-ended queue) is one of the most important features across various Standard Template Library (STL) containers. It allows the programmer to work with elements at either end of the container. The deque has the benefit of fast insertions and deletions at both ends of the container. The most common STL function used to perform an insertion at the front of the deque is the push_front member function.
In C++ , the Deque's pushfront function inserts a new element at the beginning of the deque container, and the container's size increases by one. The pushfront member function provides efficient addition of elements at the front of a deque container . It allows the coder to efficiently implement a deque in cases where queues, stacks, sliding windows, or some form of history tracking is to be simulated.
Syntax
It has the following syntax:
void push_front(value_type val);
In this syntax,
- val: New value to be added in the beginning.
- Return value: It does not return any value.
C++ Deque push_front Simple Example
Let us take a simple example to illustrate the deque push_front function in C++.
Example
#include <iostream>
#include<deque>
using namespace std; //using standard namespace
int main() //main function
{
deque<int> d={200,300,400,500};
deque<int>::iterator itr;
d.push_front(100);
for(itr=d.begin();itr!=d.end();++itr)
cout<<*itr<<" ";
return 0;
}
Output:
100 200 300 400 500
Explanation:
In this example, the push_front function adds a new element, i.e., 100, before the first element, 200.
Features of push_front
There are several features of the deque push_front function in C++. Some of them are as follows:
- In comparison to the vector, insertion into the front is a relatively quick process.
- The push_front function works well with primitive data types , strings , and user-defined classes.
- The deques keep track of blocks of memory internally, which makes insertions a relatively smooth process.
- After inserting an object at the front, it is still possible to access any part of the deque randomly.
- If the exception occurred, the deque will not have changed, which follows an exception during insertion.
Using push_front and Iterators to Traverse a Deque in C++
Let us take a simple example to illustrate how to traverse a deque using the push_front in C++.
Example
#include <iostream>
#include<deque>
using namespace std; //using standard namespace
int main() //main function
{
deque<string> d={"is","a","programming","language"};
deque<string>::iterator itr;
d.push_front("C++");
for(itr=d.begin();itr!=d.end();++itr)
cout<<*itr<<" ";
return 0;
}
Output:
C++ is a programming language
Explanation:
In this example, we demonstrate the use of a deque (double-ended queue) in C++. First, we initialize a deque of strings, which inserts "C++" at the front using the push_front function, and then iterate through the deque using an iterator to display all elements.
When to use the push_front function in C++?
The push_front function is appropriate when:
- We need the addition of newly created elements to the beginning of a sequence.
- We create data structures like double-ended queues, deques, or stacks.
- We need a way to prioritize new elements by adding them to the beginning of the sequence.
- We want to insert in reverse chronological order.
- We want to create sliding window algorithms or buffer data.
Real-World Examples
Several real world examples of deque push_front function in C++ are as follows:
- It can be used to persist the most recent user actions at the beginning.
- If we want to visit a new page, it is added to the front.
- It is very useful when we need to insert and remove elements from both ends.
Conclusion
In conclusion, the C++ deque::push_front function is commonly utilized to insert an element at the beginning of the deque. It effectively includes new elements to the front of the container without affecting existing elements, which makes it useful when frequent front insertions are required.
C++ Deque push_front Function FAQs
1) What is the difference between the pushfront and the pushback functions?
In C++, the pushfront function is used to add an element to the front of the deque, while pushback adds an element to the back of the deque. In conjunction, these function calls will allow insertion to either end of the deque efficiently compared to vectors.
2) Can push_front be used with vectors in C++?
No, the vector container does not support the push_front function in C++. Therefore, the programmer must use a deque or list for front insertions.
3) What is deque's time complexity for the push_front function?
The average time complexity for push_front is O(1) (constant time), but some of the calls may take more time due to internal memory allocation (amortized constant).
4) Is the push_front function exception-safe in C++?
Yes, the push_front function is exception-safe. Even if an exception is thrown during insertion, the deque is unchanged, and the program remains stable.
5) When do we use deque push_front rather than another container in C++?
In the C++ programming language, the push_front function with deque is very useful when we want to insert at the front of the container often, and we want random access to elements. It is preferred over vector when front insertions are required and over list when random access is needed, because list does not support direct indexing.