In C++, deque container provides fast insertion and deletion at both ends. Stands for Double Ended QUEue, it is a special type of queue where insertion and deletion operations are possible at both the ends in constant time complexity.
Example:
C++
#include <iostream>
#include <deque>
using namespace std;
int main() {
// Creating a deque of 5 elements
deque<int> dq = {1, 4, 2, 3, 5};
for (auto x: dq)
cout << x << " ";
return 0;
}
Explanation: In the above program, we created a deque of integers that contains 5 elements {1, 4, 2, 3, 5}.
Syntax
Deque is defined as the std::deque class template inside the <deque> header file.
deque<T> dq;
where,
- T: Type of elements in deque.
- dq: Name assigned to the deque.
Declaration and Initialization
Declaration and initialization are the process of creating an instance of std::deque class and assigning it some initial value. In C++, deque can be declared and initialized in multiple ways as shown in the below example:
C++
#include <bits/stdc++.h>
using namespace std;
void printD(deque<int>& dq) {
for (auto x: dq) {
cout << x << " ";
}
cout << '\n';
}
int main() {
// Creating an empty deque
deque<int> dq1;
// Creating a deque with default size and value
deque<int> dq2(3, 4);
// Creating a deque from an initializer list
deque<int> dq3 = {1, 4, 2, 3, 5};
printD(dq1);
printD(dq2);
printD(dq3);
return 0;
}
Explanation: In the above program, three deques are initialized in the following ways:
- deque<int> dq1 creates an empty deque of integers.
- deque<int> dq2(3, 4) creates a deque with 3 elements, each initialized to the value 4.
- deque<int> dq3 = {1, 4, 2, 3, 5} creates a deque and initializes it with the elements from the initializer list {1, 4, 2, 3, 5}.
Basic Operations
The basic operations on deque are shown below:
1. Inserting Elements
In deque, fast insertion and deletion are mainly performed at either ends using the push_back() and push_front() methods. Insertion at specific position can be done using insert() method.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
deque<int> dq = {1, 4, 2};
// Inserting elements at back and front
dq.push_back(5);
dq.push_front(0);
// Insert element at third position
auto it = dq.begin() + 2;
dq.insert(it, 11);
for (auto x: dq)
cout << x << " ";
return 0;
}
2. Accessing Elements
Just like arrays and vectors, deque elements can also be randomly accessed by their indexes using the operator []. Deque also provides the front()and back() methods to easily access the first and last element.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
deque<int> dq = {1, 4, 2, 3, 5};
// Accessing elements
cout << dq[2] << endl;
cout << dq.front() << endl;
cout << dq.back();
return 0;
}
3. Updating Elements
Elements in a deque can be updated by assigning the new value using assignment operator while accessing the element by index.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
deque<int> dq = {1, 4, 2, 3, 5};
// Updating element
dq[2] = 8;
cout << dq[2];
return 0;
}
Explanation: In the above code, dq[2] = 8 directly changes the element at index 2 to 8. This can be verified in the program's output.
4. Traversing
Deque can be traversed by simply using a loop in which each element is accessed using its index. In deque, the indexes start from 0 and goes till deque.size() - 1. where the function size() returns the current number of elements in the deque.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
deque<int> dq = {1, 4, 2, 3, 5};
// Traversing using the for loop
for (int i = 0; i < dq.size(); i++)
cout << dq[i] << " ";
return 0;
}
Explanation: In the above code, traversal through the deque is done using a for loop and index-based access dq[i] to visit each element sequentially until i is less than the size of the deque.
The other method is to just use the range based for loop like we have done in previous examples.
5. Deleting Elements
We can remove elements from the back and front of the deque using the pop_back() and pop_front() methods. For removing element at specific index, the erase() method is used.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
deque<int> dq = {1, 4, 2, 3, 5};
// Deleting from the back and front
dq.pop_back();
dq.pop_front();
dq.erase(dq.begin());
for (int i = 0; i < dq.size(); i++)
cout << dq[i] << " ";
return 0;
}
Time Complexity
The below table lists the time complexity of the above operations on deque:
Operation | Time Complexity |
---|
Insert at back | O(1) amortized |
Insert at front | O(1) amortized |
Insert at arbitrary position | O(n) |
Remove from back | O(1) amortized |
Remove from front | O(1) amortized |
Remove from arbitrary position | O(n) |
Access elements at any position using index | O(1) |
Update elements at any position using index | O(1) |
Iterate the deque | O(n) |
Other Common Operations
Deque is used in many situations for different purposes. The following examples' aim is to help you master deque beyond the basics:
Internal Working of Deque
A deque is implemented using a dynamic array or a set of fixed-size arrays. Unlike a vector, which stores elements in a contiguous memory block, a deque allows quick insertion and deletion at both ends by maintaining a multiple memory blocks. Refer to this article to know more - Internal Working of Deque
Queue vs Deque
The main difference between a queue and a deque is that:
- In queue, elements are inserted into one end of the container and deleted from the other.
- On the other hand, deque allow both insertion and deletion to both ends. It also allows random access.
All Member Functions of Deque
Following is the list of all member functions of std::deque class in C++:
Function | Description |
---|
push_front() | Add an element to the front of the deque. |
---|
push_back() | Adds an element to the end of the deque. |
---|
pop_front() | Remove the first element of the deque. |
---|
pop_back() | Removes the last element of the deque. |
---|
size() | Returns the number of elements in the deque. |
---|
max_size() | Returns the maximum number of elements that the deque can hold. |
---|
resize() | Changes the size of the deque. |
---|
empty() | Checks if the deque is empty. |
---|
at() | Accesses the element at a specific position, with bounds checking. |
---|
front() | Accesses the first element of the deque. |
---|
back() | Accesses the last element of the deque. |
---|
begin() | Returns an iterator pointing to the first element of the deque. |
---|
end() | Returns an iterator pointing to the beyond of the last element of the deque. |
---|
rbegin() | Returns a reverse iterator pointing to the last element of the deque. |
---|
rend() | Returns a reverse iterator pointing to the element preceding the first element of the deque. |
---|
cbegin() | Returns const_iterator to beginning |
---|
cend() | Returns const_iterator to end |
---|
crbegin() | Returns const_reverse_iterator to reverse beginning |
---|
crend() | Returns const_reverse_iterator to reverse end |
---|
insert() | Inserts elements at a specific position in the deque. |
---|
erase() | Removes elements from a specific position or range in the deque. |
---|
swap() | Swaps the contents of the deque with another deque. |
---|
clear() | Removes all elements from the deque. |
---|
emplace() | function inserts a new element just before the specified position |
---|
emplace_front() | Constructs and inserts an element at the front of the deque. |
---|
emplace_back() | Constructs and inserts an element at the end of the deque. |
---|
assign() | Assigns new values to the deque elements by replacing old ones. |
---|
shrink_to_fit() | Reduces memory usage by freeing unused space. |
---|
get_allocator() | Returns a copy of the allocator object associated with the deque. |
---|
Similar Reads
Deque in C++ STL
In C++, deque container provides fast insertion and deletion at both ends. Stands for Double Ended QUEue, it is a special type of queue where insertion and deletion operations are possible at both the ends in constant time complexity.Example:C++#include <iostream> #include <deque> using
6 min read
Commonly Used Methods
deque::push_front() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
2 min read
deque::push_back() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
2 min read
deque::pop_front() and deque::pop_back() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
4 min read
Deque::front() and deque::back() in C++ STL
Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may
4 min read
deque insert() function in C++ STL
The deque::insert() function is a built-in function in C++ which is used to insert elements in the deque. The insert() function can be used in three ways: Extends deque by inserting a new element val at a position.Extends deque by inserting n new element of value val in the deque.Extends deque by in
3 min read
deque::begin() and deque::end in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
3 min read
Deque::empty() and deque::size() in C++ STL
Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may
4 min read
deque::clear() and deque::erase() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation
5 min read
Other Member Methods
deque max_size() function in C++ STL
The deque::max_size() is a built-in function in C++ STL which returns the maximum number of elements that a deque container can hold. Syntax: deque_name.max_size()Parameters: The function does not accept any parameters. Return Value: The function returns the maximum number of elements that a deque c
1 min read
deque assign() function in C++ STL
The deque::assign() is a built-in function in C++ STL which is used to assign values to the same or different deque container. On being called more than once in the same program, the function destroys the values of the previous elements and re-assigns new set of elements to the container. Syntax: de
2 min read
deque rbegin() function in C++ STL
The deque::rbegin() is an inbuilt function in C++ STL which returns a reverse iterator which points to the last element of the deque (i.e., its reverse beginning). Syntax: deque_name.rbegin()Parameter: This function does not accept any parameters. Return value: It returns a reverse iterator which po
2 min read
deque rend() function in C++ STL
The deque::rend() is an inbuilt function in C++ STL which returns a reverse iterator which points to the position before the beginning of the deque (which is considered its reverse end). Syntax: deque_name.rend()Parameter: This function does not accept any parameters. Return value: It returns a reve
2 min read
deque cbegin() in C++ STL
The cbegin() method in deque is a function in C++ STL which returns an iterator pointing to the first element of the container. Syntax: deque_name.cbegin() Return value: It returns a constant iterator pointing to the first element of the deque. This means, that the iterator can be used to traverse t
2 min read
deque cbegin() in C++ STL
The cbegin() method in deque is a function in C++ STL which returns an iterator pointing to the first element of the container. Syntax: deque_name.cbegin() Return value: It returns a constant iterator pointing to the first element of the deque. This means, that the iterator can be used to traverse t
2 min read
deque::operator= and deque::operator[] in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
4 min read
deque::at() and deque::swap() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
4 min read
How Deque Works Internally in C++?
Prerequisite: Deque in C++ Deque or Double Ended Queue is a generalized version of the Queue data structure that allows insert and deletion at both ends. It supports the access of elements in O(1) time complexity and the insertion and deletion of elements from front and back are both done with O(1)
4 min read
Deque of Pairs in C++ with Examples
What is a deque? In C++, the deque is a sequence container and it is also known by the name, double-ended queue. As the name implies, a deque allows insertion and deletion from both ends. Although a deque is similar to a vector, deques are more efficient compared to vectors. In vectors, contiguous s
5 min read
Difference between Queue and Deque in C++
Queue: A Queue is a linear data structure that follows a First In First Out (FIFO) order in which the operations are performed. It is a type of container adaptor where elements are inserted into one end of the container and deleted from the other. Functions: empty(): Tests whether the queue is empty
4 min read
Deque vs Vector in C++ STL
Deque in C++ Standard Template Library (STL) Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors but support inserting and deleting the first element in O(1). Unlike vectors, contiguous storage allocation is not guarante
2 min read
How to check/find an item in Dequeue using find() method
find() function finds the element in the given range of numbers. Returns an iterator to the first element in the range [first, last) that compares equal to the value to be searched. If no such element is found, the function returns last. Syntax: InputIterator find (InputIterator first, InputIterator
6 min read