Difference between Queue and Deque in C++ Last Updated : 20 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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.size(): Returns the unsigned int, size of the queue.queue::front() and queue::back(): front() function returns a reference to the first element or the oldest of the queue. back() function returns a reference to the last or the newest element of the queue.push(k) and pop(): push() function adds the element âkâ at the end of the queue. pop() function deletes the element from the beginning of the queue and reduces its size by 1.swap(): exchanges the elements of two different queues of the same type but may or may not of the same size.emplace(): it is used to insert a new element at end of the queue. Syntax: queue <data_type> q Below is the program to illustrate the same: C++ // C++ program to demonstrate the // working of queue #include <bits/stdc++.h> using namespace std; // Driver Code int main() { // Declare a queue queue<int> q; // Insert elements in the queue q.push(10); q.push(5); q.push(15); q.push(1); // Delete elements from the queue q.pop(); q.pop(); cout << "Elements in Queue are: "; // Print the element stored // in queue while (!q.empty()) { cout << q.front() << ' '; // Pop the front element q.pop(); } return 0; } Output: Elements in Queue are: 15 1 Deque: Deque is a sequence container with the ability of expansion and contraction on both ends. It is a template of Standard Template Library or STL in C++is. It is similar to vectors but are more efficient for the insertion and deletion of elements. Contiguous storage allocation in deque may not be guaranteed as in vectors. Functions: max_size(): Returns the maximum number of elements deque can contain.push_back() and push_front(): push_front( ) push the elements into a deque from the front and push_back( ) push elements into a deque from the back.pop_front() and pop_back(): pop_front() function is used to pop elements from a deque from the front and pop_back( ) function is used to pop elements from a deque from the back.clear() and erase(): clear is used to remove all the elements from the deque and erase is used to remove some specified elements.insert(): increases the container side by inserting element in the specified position.resize(): changes the size of the element's container as per requirement.rbegin() and rend(): rbegin() points to the last element of the deque whereas rend points to the position before the beginning of the deque.at() and swap(): at() points to the position of the element given in the parameter and swap( ) is used two swap elements of two deques.emplace_front() and emplace_back(): these two functions are used to insert new elements in the container at the beginning and at the end of deque respectively. Syntax: deque<data_type> dq Below is the program to illustrate the same: C++ // C++ program to demonstrate the // working of deque #include <bits/stdc++.h> using namespace std; // Driver Code int main() { // Declare a deque deque<int> dq; // Insert element in the front dq.push_front(10); dq.push_front(5); dq.push_front(3); // Delete elements from the front dq.pop_front(); dq.pop_front(); // Insert elements in the back dq.push_back(1); dq.push_back(50); dq.push_back(2); // Delete elements from the back dq.pop_back(); dq.pop_back(); cout << "Elements in deque are: "; // Print the element stored // in deque while (!dq.empty()) { cout << " " << dq.front(); dq.pop_front(); } return 0; } Output: Elements in deque are: 10 1 Below is the tabular difference between the queue and deque: S.No.Queue Deque 1Insertion can be done through the rear end only.Insertion is possible through both ends.2Deletion of elements is possible through the front end only.Deletion of elements possible through both ends.3Elements can not be accessed through iterators.Elements can be accessed through iterators.4Implemented as container adaptors.Implemented generally as some form of a dynamic array.5The stack can not be implemented using a queue.A stack can be implemented using deque. Comment More infoAdvertise with us Next Article Deque vs Vector in C++ STL A aktmishra143 Follow Improve Article Tags : C++ cpp-deque deque Practice Tags : CPPDeque 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 Methodsdeque::push_front() in C++ STLDeque 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++ STLDeque 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++ STLDeque 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++ STLDeque 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++ STLThe 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++ STLDeque 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++ STLDeque 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++ STLDeque 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 Methodsdeque max_size() function in C++ STLThe 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++ STLThe 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++ STLThe 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++ STLThe 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++ STLThe 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++ STLThe 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++ STLDeque 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++ STLDeque 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 Like