Namespaces
Variants

std::ranges:: lexicographical_compare

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:: input_iterator I1, std:: sentinel_for < I1 > S1,

std:: input_iterator I2, std:: sentinel_for < I2 > S2,
class Proj1 = std:: identity , class Proj2 = std:: identity ,
std:: indirect_strict_weak_order <
std :: projected < I1, Proj1 > ,
std :: projected < I2, Proj2 >> Comp = ranges:: less >
constexpr bool
lexicographical_compare ( I1 first1, S1 last1, I2 first2, S2 last2,

Comp comp = { } , Proj1 proj1 = { } , Proj2 proj2 = { } ) ;
(1) (C++20 이후)
template < ranges:: input_range R1, ranges:: input_range R2,

class Proj1 = std:: identity , class Proj2 = std:: identity ,
std:: indirect_strict_weak_order <
std :: projected < ranges:: iterator_t < R1 > , Proj1 > ,
std :: projected < ranges:: iterator_t < R2 > , Proj2 >> Comp = ranges:: less >
constexpr bool
lexicographical_compare ( R1 && r1, R2 && r2, Comp comp = { } ,

Proj1 proj1 = { } , Proj2 proj2 = { } ) ;
(2) (C++20 이후)

첫 번째 범위 [ first1 , last1 ) 가 두 번째 범위 [ first2 , last2 ) 보다 사전순으로 작은지 확인합니다.

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

사전식 비교는 다음과 같은 속성을 가진 연산입니다:

  • 두 범위는 요소별로 비교됩니다.
  • 첫 번째 불일치하는 요소가 어떤 범위가 사전식으로 더 작은지 또는 더 큰지 를 정의합니다.
  • 한 범위가 다른 범위의 접두사인 경우, 더 짧은 범위가 사전식으로 더 작습니다 .
  • 두 범위가 동등한 요소를 가지고 길이가 같은 경우, 범위는 사전식으로 동일합니다 .
  • 빈 범위는 사전식으로 모든 비어 있지 않은 범위보다 더 작습니다 .
  • 두 빈 범위는 사전식으로 동일합니다 .

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

목차

매개변수

first1, last1 - 검사할 첫 번째 범위 를 정의하는 반복자-감시자 쌍
r1 - 검사할 첫 번째 요소 범위
first2, last2 - 검사할 두 번째 범위 를 정의하는 반복자-감시자 쌍
r2 - 검사할 두 번째 요소 범위
comp - 투영된 요소에 적용할 비교 함수
proj1 - 첫 번째 요소 범위에 적용할 투영
proj2 - 두 번째 요소 범위에 적용할 투영

반환값

true 첫 번째 범위가 두 번째 범위보다 사전순으로 작은 경우.

복잡도

최대 2·min(N1, N2) 번의 비교 연산과 해당 프로젝션 연산이 수행되며, 여기서 N1 = ranges:: distance ( first1, last1 ) 이고 N2 = ranges:: distance ( first2, last2 ) 입니다.

가능한 구현

struct lexicographical_compare_fn
{
    template<std::input_iterator I1, std::sentinel_for<I1> S1,
             std::input_iterator I2, std::sentinel_for<I2> S2,
             class Proj1 = std::identity, class Proj2 = std::identity,
             std::indirect_strict_weak_order<
                 std::projected<I1, Proj1>,
                 std::projected<I2, Proj2>> Comp = ranges::less>
    constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2,
                              Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        for (; (first1 != last1) && (first2 != last2); ++first1, (void) ++first2)
        {
            if (std::invoke(comp, std::invoke(proj1, *first1), std::invoke(proj2, *first2)))
                return true;
            if (std::invoke(comp, std::invoke(proj2, *first2), std::invoke(proj1, *first1)))
                return false;
        }
        return (first1 == last1) && (first2 != last2);
    }
    template<ranges::input_range R1, ranges::input_range R2,
             class Proj1 = std::identity, class Proj2 = std::identity,
             std::indirect_strict_weak_order<
                 std::projected<ranges::iterator_t<R1>, Proj1>,
                 std::projected<ranges::iterator_t<R2>, Proj2>> Comp = ranges::less>
    constexpr bool operator()(R1&& r1, R2&& r2, Comp comp = {},
                              Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        return (*this)(ranges::begin(r1), ranges::end(r1),
                       ranges::begin(r2), ranges::end(r2),
                       std::ref(comp), std::ref(proj1), std::ref(proj2));
    }
};
inline constexpr lexicographical_compare_fn lexicographical_compare;

예제

#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>
int main()
{
    std::vector<char> v1 {'a', 'b', 'c', 'd'};
    std::vector<char> v2 {'a', 'b', 'c', 'd'};
    namespace ranges = std::ranges;
    auto os = std::ostream_iterator<char>(std::cout, " ");
    std::mt19937 g {std::random_device {}()};
    while (not ranges::lexicographical_compare(v1, v2))
    {
        ranges::copy(v1, os);
        std::cout << ">= ";
        ranges::copy(v2, os);
        std::cout << '\n';
        ranges::shuffle(v1, g);
        ranges::shuffle(v2, g);
    }
    ranges::copy(v1, os);
    std::cout << "<  ";
    ranges::copy(v2, os);
    std::cout << '\n';
}

가능한 출력:

a b c d >= a b c d
d a b c >= c b d a
b d a c >= a d c b
a c d b <  c d a b

참고 항목

두 원소 집합이 동일한지 판별
(알고리즘 함수 객체)
한 범위가 다른 범위보다 사전순으로 작으면 true 반환
(함수 템플릿)