Namespaces
Variants

std::ranges:: is_sorted

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
헤더 파일에 정의됨 <algorithm>
함수 시그니처
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 = { } ) ;
(1) (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 = { } ) ;
(2) (C++20 이후)

범위 내 요소들이 비내림차순으로 정렬되어 있는지 확인합니다. [ first , last )

시퀀스는 비교자 comp 에 대해 정렬되어 있습니다. 만약 시퀀스를 가리키는 임의의 반복자 it it + n 가 시퀀스의 요소를 가리키는 유효한 반복자인 임의의 음이 아닌 정수 n 에 대해, std:: invoke ( comp, std:: invoke ( proj, * ( it + n ) ) , std:: invoke ( proj, * it ) ) false 로 평가되는 경우입니다.

1) 요소들은 주어진 이항 비교 함수 comp 를 사용하여 비교됩니다.
2) (1) 과 동일하지만, r 을 소스 범위로 사용하며, 마치 ranges:: begin ( r ) first 로, ranges:: end ( r ) last 로 사용하는 것과 같습니다.

이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:

목차

매개변수

first, last - 정렬되었는지 확인할 요소들의 범위 를 정의하는 반복자-감시자 쌍
r - 정렬되었는지 확인할 요소들의 범위
comp - 투영된 요소들에 적용할 비교 함수
proj - 요소들에 적용할 투영

반환값

true 범위 내 요소들이 comp 에 따라 정렬된 경우.

복잡도

first last 사이의 거리에 선형적으로 비례합니다.

가능한 구현

struct is_sorted_fn
{
    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 operator()(I first, S last, Comp comp = {}, Proj proj = {}) const
    {
        return ranges::is_sorted_until(first, last, comp, proj) == last;
    }
    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 operator()(R&& r, Comp comp = {}, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::ref(comp), std::ref(proj));
    }
};
inline constexpr is_sorted_fn is_sorted;

참고 사항

ranges::is_sorted true 를 빈 범위와 길이가 1인 범위에 대해 반환합니다.

예제

#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";
}

출력:

3 1 4 1 5 : not sorted
1 1 3 4 5 : sorted
5 4 3 1 1 : sorted (with 'greater')

참고 항목

가장 큰 정렬된 하위 범위를 찾음
(알고리즘 함수 객체)
(C++11)
범위가 오름차순으로 정렬되었는지 확인
(함수 템플릿)