![]() |
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 30 40 50 Sort Elements in a Deque in C++We can use the std::sort method provided by the STL library to sort a std::deque in C++. This function sorts the elements in a given range into ascending order by default. Syntax of std::sortsort(deque.begin(), deque.end())
where begin and end are iterators denoting the beginning and the end of the deque. If we want to change the order, we can provide the custom comparator as third parameter. C++ Program to Sort a DequeThe following code demonstrates how we can sort a deque in C++:
Output Original deque: 30 10 20 50 40 Sorted deque: 10 20 30 40 50 Time Complexity: O(N logN) where N is the number of elements in the deque. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |