std::ranges::is_sorted
Min standard notice:
Header: <algorithm>
Checks if the elements in range [first,last) are sorted in non-descending order.
# Declarations
Call signature
template< std::forward_iterator I, std::sentinel_for<I> S,
class Proj = std::identity,
std::indirect_strict_weak_order<std::projected<I, Proj>>
Comp = ranges::less >
constexpr bool
is_sorted( I first, S last, Comp comp = {}, Proj proj = {} );
(since C++20)
template< ranges::forward_range R, class Proj = std::identity,
std::indirect_strict_weak_order<
std::projected<ranges::iterator_t<R>, Proj>>
Comp = ranges::less >
constexpr bool
is_sorted( R&& r, Comp comp = {}, Proj proj = {} );
(since C++20)
# Parameters
first, last: iterator-sentinel defining the range to check if it is sortedr: the range to check if it is sortedcomp: comparison function to apply to the projected elementsproj: projection to apply to the elements
# Return value
true if the elements in the range are sorted according to comp.
# Notes
ranges::is_sorted returns true for empty ranges and ranges of length one.
# Example
#include <algorithm>
#include <array>
#include <functional>
#include <iostream>
#include <iterator>
int main()
{
namespace ranges = std::ranges;
std::array digits {3, 1, 4, 1, 5};
ranges::copy(digits, std::ostream_iterator<int>(std::cout, " "));
ranges::is_sorted(digits)
? std::cout << ": sorted\n"
: std::cout << ": not sorted\n";
ranges::sort(digits);
ranges::copy(digits, std::ostream_iterator<int>(std::cout, " "));
ranges::is_sorted(ranges::begin(digits), ranges::end(digits))
? std::cout << ": sorted\n"
: std::cout << ": not sorted\n";
ranges::reverse(digits);
ranges::copy(digits, std::ostream_iterator<int>(std::cout, " "));
ranges::is_sorted(digits, ranges::greater {})
? std::cout << ": sorted (with 'greater')\n"
: std::cout << ": not sorted\n";
}