How to Reverse a Deque in C++? Last Updated : 27 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++ STL, we have a container called deque(short for double-ended queue) that allows fast insertion and deletion operations at both the beginning and end. In this article, we will learn how to reverse a deque in C++. Example: Input: myDeque = {1, 2, 3, 4, 5}; Output: Reversed Deque: 5 4 3 2 1Reverse Deque in C++ STLTo reverse a std::deque in C++, we can use the std::reverse() function from the <algorithm> library that reverses the order of elements in a given range. We need to pass the beginning and end iterators of the deque to this function. Syntax of std::reverse()reverse(first, last);Here, first is the iterator to the beginning of the deque.last is the iterator to the end of the deque.C++ Program to Reverse a DequeThe below example demonstrates how we can reverse a deque in C++. C++ // C++ Program to demonstrate how we can reverse a deque #include <algorithm> #include <deque> #include <iostream> using namespace std; int main() { // Creating a deque deque<int> dq = { 1, 2, 3, 4, 5 }; // Printing the original deque cout << "Original Deque: "; for (int num : dq) { cout << num << " "; } cout << endl; // Reversing the deque reverse(dq.begin(), dq.end()); // Printing the reversed deque cout << "Reversed Deque: "; for (int num : dq) { cout << num << " "; } cout << endl; return 0; } OutputOriginal Deque: 1 2 3 4 5 Reversed Deque: 5 4 3 2 1 Time Complexity: O(N), here N is the number of elements in deque.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Reverse a Deque in C++? G gauravgandal Follow Improve Article Tags : C++ Programs C++ STL cpp-deque CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Sort a Deque in C++? In C++, the STL provides a container called a double-ended queue which is popularly known as deque. This container allows fast insertions and deletions at both ends of the container. In this article, we will learn how to sort a deque in C++. Example: Input: myDeque = {30, 10, 20,50,40} Output: 10 20 2 min read How to Reverse a Stack in C++? In C++, stacks are containers that store the elements in the last in-first out order(LIFO). In, this article, we will learn how we can reverse a stack in C++. Example Input: stack<int> S ={5,4,3,2,1} store Output: // Reversed Stack stack<int> S ={1,2,3,4,5}Reverse a Stack in C++We can re 2 min read How to Reverse a String in C++? Reversing a string means replacing the first character with the last character, second character with the second last character and so on. In this article, we will learn how to reverse a string in C++.ExamplesInput: str = "Hello World"Output: dlroW olleHExplanation: The last character is replaced by 2 min read How to Reverse a List in C++ STL? In C++, std::list is a sequence container that allows non-contiguous memory allocation. As such, it is a doubly linked list that can be traversed in both directions. In this article, we will learn how to reverse a list in C++. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Reversed List: 50 2 min read How to Create a Deque of Sets in C++? In C++, a container called deque is a queue like container but allows for fast insertions and deletions at both ends. In this article, we will learn how to create a deque of sets in C++. Example: Input: mySet1 = {1, 4, 8, 9, 11} mySet2 = {1, 2, 3, 5, 7} Output: myDeque: [ {1, 4, 8, 9, 11}, {1, 2, 3, 2 min read Like