Namespaces
Variants

std::ranges::lexicographical_compare

cppreference.net에서
 
 
알고리즘 라이브러리
제약 알고리즘과 범위 기반 알고리즘 (C++20)
제약 알고리즘, 예: ranges::copy, ranges::sort, ...
정렬 및 관련 연산
분할 연산
(C++11)    

정렬 연산
이진 탐색 연산
(분할된 범위에서)
집합 연산 (정렬된 범위에서)
병합 연산 (정렬된 범위에서)
힙 연산
최소/최대 연산
(C++11)
(C++17)
사전식 비교 연산
순열 연산


 
제약 알고리즘
이 메뉴의 모든 이름은 std::ranges
네임스페이스에 속함
(C++23)
unique_copy
stable_partition
partial_sort_copy
equal_range
set_symmetric_difference
pop_heap
clamp
prev_permutation
fold_left
fold_left_first  
fold_right
fold_right_last  
fold_left_with_iter
fold_left_first_with_iter
(C++23)
iota            
(C++23)
generate_random
(C++26)
uninitialized_value_construct_n
 
(C++23)<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 = {} );
에 정의됨 호출 시그니처
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 = {} );
(1) (C++20부터)

(2) [first1, last1)(C++20부터)첫 번째 범위 가 두 번째 범위 [first2, last2)보다 사전식으로

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

를

  • 로 사용하는 것과 같습니다.
  • 사전식 비교는 다음과 같은 속성을 가진 연산입니다: 두 범위는 요소별로 비교됩니다.첫 번째로 일치하지 않는 요소가 어떤 범위가 사전식으로 작은지 또는
  • 큰지를 결정합니다.한 범위가 다른 범위의 접두사라면, 더 짧은 범위가 사전식으로
  • 작습니다.두 범위가 동등한 요소들을 가지고 길이가 같다면, 두 범위는 사전식으로 같습니다
  • .빈 범위는 비어 있지 않은 범위보다 사전식으로 작습니다.
  • 두 빈 범위는 사전식으로 같습니다.

이 페이지에 설명된 함수형 개체들은 알고리즘 함수 객체 (비공식적으로 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

참고 항목

두 요소 집합이 같은지 판별합니다
(알고리즘 함수 객체)
두 범위를 사전순으로 비교합니다
(함수 템플릿)