Namespaces
Variants

std::lexicographical_compare_three_way

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

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


 
헤더 <algorithm>
template< class InputIt1, class InputIt2, class Cmp >
constexpr auto lexicographical_compare_three_way
    ( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
      Cmp comp ) -> decltype(comp(*first1, *first2));
에 정의됨 (1) (C++20부터)
template< class InputIt1, class InputIt2 >
constexpr auto lexicographical_compare_three_way
    ( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 );
(2) (C++20부터)

두 범위 [first1, last1)과 [first2, last2)를 삼진 비교를 사용하여 사전식으로 비교하고 가장 강력한 적용 가능 비교 범주 유형의 결과를 생성합니다.

1) 두 범위에서 comp에 따라 첫 번째 동등하지 않은 요소 쌍이 있으면 그 순서를 반환하고, 그렇지 않으면 (한 범위가 comp에 따라 다른 범위의 접두사와 동등한 경우) 두 범위 길이의 순서를 반환합니다.
2) return std::lexicographical_compare_three_way(
first1, last1, first2, last2, std::compare_three_way());

와 동일합니다.반환 유형이 세 가지 비교 범주 유형 중 하나가 아니면 프로그램의 형식이 잘못되었습니다:

매개변수

first1, last1 - 검사할 첫 번째 범위의 요소를 정의하는 반복자 쌍
first2, last2 - 검사할 두 번째 범위의 요소를 정의하는 반복자 쌍
comp - 함수 객체
유형 요구 사항
-
InputIt1, InputIt2는 LegacyInputIterator의 요구 사항을 충족해야 합니다.

반환값

위에 지정된 비교 범주 유형의 값입니다.

복잡도

N1을 std::distance(first1, last1)로, N2을 std::distance(first2, last2)로 할 때:

1) 최대 min(1,N2)번의 comp 적용.
2) 최대 min(N1,N2)번의 std::compare_three_way() 적용.

가능한 구현

template<class I1, class I2, class Cmp>
constexpr auto lexicographical_compare_three_way(I1 f1, I1 l1, I2 f2, I2 l2, Cmp comp)
    -> decltype(comp(*f1, *f2))
{
    using ret_t = decltype(comp(*f1, *f2));
    static_assert(std::disjunction_v<
                      std::is_same<ret_t, std::strong_ordering>,
                      std::is_same<ret_t, std::weak_ordering>,
                      std::is_same<ret_t, std::partial_ordering>>,
                  "The return type must be a comparison category type.");
    
    bool exhaust1 = (f1 == l1);
    bool exhaust2 = (f2 == l2);
    for (; !exhaust1 && !exhaust2; exhaust1 = (++f1 == l1), exhaust2 = (++f2 == l2))
        if (auto c = comp(*f1, *f2); c != 0)
            return c;
    
    return !exhaust1 ? std::strong_ordering::greater:
           !exhaust2 ? std::strong_ordering::less:
                       std::strong_ordering::equal;
}

예제

#include <algorithm>
#include <cctype>
#include <compare>
#include <iomanip>
#include <iostream>
#include <string_view>
#include <utility>

using namespace std::literals;

void show_result(std::string_view s1, std::string_view s2, std::strong_ordering o)
{
    std::cout << std::quoted(s1) << " is ";
    std::is_lt(o) ? std::cout << "less than ":
    std::is_gt(o) ? std::cout << "greater than ":
                    std::cout << "equal to ";
    std::cout << std::quoted(s2) << '\n';
}

std::strong_ordering cmp_icase(unsigned char x, unsigned char y)
{
    return std::toupper(x) <=> std::toupper(y);
};
    
int main()
{
    for (const auto& [s1, s2] :
    {
        std::pair{"one"sv, "ONE"sv}, {"two"sv, "four"sv}, {"three"sv, "two"sv}
    })
    {
        const auto res = std::lexicographical_compare_three_way(
            s1.cbegin(), s1.cend(), s2.cbegin(), s2.cend(), cmp_icase);
        show_result(s1, s2, res);
    }
}

출력:

"one" is equal to "ONE"
"two" is greater than "four"
"three" is less than "two"

결함 보고서

다음 동작 변경 결함 보고서가 이전에 게시된 C++ 표준에 소급하여 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 3410 C++20 반복자 간 불필요한 비교가 필요했습니다 해당 요구 사항이 제거됨

참고 항목

두 범위를 사전식으로 비교합니다
(함수 템플릿 & 알고리즘 함수 객체)
x <=> y
을 구현하는 제약된 함수 객체 (클래스)
두 범위를 사전식으로 비교합니다
(알고리즘 함수 객체)