Namespaces
Variants

std:: lexicographical_compare_three_way

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
lexicographical_compare_three_way
(C++20)
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
헤더 파일에 정의됨 <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 ) 를 삼중 비교(three-way comparison)를 사용하여 비교하고, 적용 가능한 가장 강력한 비교 범주 타입의 결과를 생성합니다.

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 요구사항을 충족해야 합니다.

반환값

위에서 명시된 비교 범주 유형의 값입니다.

복잡도

N 1 std:: distance ( first1, last1 ) 로, N 2 std:: distance ( first2, last2 ) 로 정의할 때:

1) 최대 min( 1 ,N 2 ) 번의 comp 적용.
2) 최대 min(N 1 ,N 2 ) 번의 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>>,
                  "반환 타입은 비교 범주 타입이어야 합니다.");
    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 반복자 간의 불필요한 비교가 요구됨 해당 요구사항 제거됨

참고 항목

한 범위가 다른 범위보다 사전식으로 작으면 true 를 반환함
(함수 템플릿)
x <=> y 를 구현하는 제약된 함수 객체
(클래스)
한 범위가 다른 범위보다 사전식으로 작으면 true 를 반환함
(알고리즘 함수 객체)