In C++, is_sorted() is a built-in function used to check whether the element of the given range is sorted or not in ascending order. In this article, we will learn about is_sorted() function in C++.
Let’s take a quick look at a simple example that illustrates is_sorted() method:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 5, 6, 8, 9};
// Checking if vector v is sorted or not
if (is_sorted(v.begin(), v.end()))
cout << "Sorted";
else
cout << "Not Sorted";
return 0;
}
OutputGiven Vector is Sorted
Explanation: By default, is_sorted() function checks whether the range is sorted or not in ascending order. As the vector v is sorted in ascending, the function returned true. Let's look at is_sorted() in detail.
This article covers the syntax, usage, and common examples of is_sorted() method in C++:
Syntax of is_sorted()
The is_sorted() is defined inside <algorithm> header file.
is_sorted(first, last);
is_sorted(first, last, comp);
Parameters
- first: Iterator to the first element of given range.
- last: Iterator to the element just after the last element of given range.
- comp(optional): It is a custom comparison function used to change the sorting order to be checked. By default, it uses the < operator which check if the range is sorted in ascending order.
Return Value
- Returns true, if the range is sorted in the given order.
- Returns false, if the range is not sorted in given order.
Examples of is_sorted()
The following examples demonstrates the use of is_sorted() method in different scenario:
Check if an Array is Sorted in Ascending Order
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = {4, 7, 8, 9};
int n = sizeof(arr)/sizeof(arr[0]);
// Check whether array is sorted in ascending
// order
if (is_sorted(arr, arr + n))
cout << "Sorted";
else
cout << "Not Sorted";
return 0;
}
OutputGiven Array is Sorted
Check if a List is Sorted in Descending Order
C++
#include <bits/stdc++.h>
using namespace std;
// Comparator function
bool comp(int a, int b) {
return a > b;
}
int main() {
list<int> l = {9, 7, 6, 3};
// Check vector Sorted in descending order
if (is_sorted(l.begin(), l.end(), comp))
cout << "Sorted";
else
cout << "Not Sorted";
return 0;
}
OutputGiven Vector is Sorted
Explanation: As by default, is_sorted() checks for ascending order, we have to provide custom comparison function to check for descending order.
Check if Vector of String is in Lexicographically Ordered
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<string> v = {"hi", "geeks", "welcome", "to" "geeksforgeeks"};
// Check vector sorted in user
// defined order or not
if (is_sorted(v.begin(), v.end()))
cout << "Sorted";
else
cout << "Not Sorted";
return 0;
}
OutputGiven Vector is Sorted
Check if Set is Sorted in Descending Order
C++
#include <bits/stdc++.h>
using namespace std;
// Comparator function
bool comp(int a, int b) {
return a > b;
}
int main() {
set<int> s = {9, 7, 6, 3};
// Check vector Sorted in descending order
if (is_sorted(s.begin(), s.end(), comp))
cout << "Sorted";
else
cout << "Not Sorted";
return 0;
}
Explanation: The set is already sorted in ascending order. We have used is_sorted() with custom comparator to check if the set is in descending order.
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems