Namespaces
Variants

std::ranges:: is_sorted_until

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 I

is_sorted_until ( I first, S last, Comp comp = { } , Proj proj = { } ) ;
(1) (C++20 이후)
template < std:: forward_range R, class Proj = std:: identity ,

std:: indirect_strict_weak_order <
std :: projected < ranges:: iterator_t < R > , Proj >>
Comp = ranges:: less >
constexpr ranges:: borrowed_iterator_t < R >

is_sorted_until ( R && r, Comp comp = { } , Proj proj = { } ) ;
(2) (C++20 이후)

범위 [ first , last ) 를 검사하고 first 에서 시작하는 요소들이 비내림차순으로 정렬된 가장 큰 범위를 찾습니다.

시퀀스는 비교자 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 - 요소들에 적용할 투영

반환값

first 에서 시작하는 가장 큰 범위의 상한으로, 이 범위 내의 요소들은 비내림차순으로 정렬되어 있습니다. 즉, 범위 [ first , it ) 가 정렬된 상태인 마지막 반복자 it 를 의미합니다.

복잡도

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

가능한 구현

struct is_sorted_until_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 I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const
    {
        if (first == last)
            return first;
        for (auto next = first; ++next != last; first = next)
            if (std::invoke(comp, std::invoke(proj, *next), std::invoke(proj, *first)))
                return next;
        return first;
    }
    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 ranges::borrowed_iterator_t<R>
        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_until_fn is_sorted_until;

참고 사항

ranges::is_sorted_until 는 빈 범위와 길이가 1인 범위에 대해 last 와 동일한 반복자를 반환합니다.

예제

#include <array>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
int main()
{
    std::random_device rd;
    std::mt19937 g {rd()};
    std::array nums {3, 1, 4, 1, 5, 9};
    constexpr int min_sorted_size = 4;
    int sorted_size = 0;
    do
    {
        std::ranges::shuffle(nums, g);
        const auto sorted_end = std::ranges::is_sorted_until(nums);
        sorted_size = std::ranges::distance(nums.begin(), sorted_end);
        std::ranges::copy(nums, std::ostream_iterator<int>(std::cout, " "));
        std::cout << " : " << sorted_size << " leading sorted element(s)\n";
    }
    while (sorted_size < min_sorted_size);
}

가능한 출력:

4 1 9 5 1 3  : 1 leading sorted element(s)
4 5 9 3 1 1  : 3 leading sorted element(s)
9 3 1 4 5 1  : 1 leading sorted element(s)
1 3 5 4 1 9  : 3 leading sorted element(s)
5 9 1 1 3 4  : 2 leading sorted element(s)
4 9 1 5 1 3  : 2 leading sorted element(s)
1 1 4 9 5 3  : 4 leading sorted element(s)

참고 항목

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