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 ) ,并生成适用比较类别类型中最强类型的结果。

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) << " 是 ";
    std::is_lt(o) ? std::cout << "小于 ":
    std::is_gt(o) ? std::cout << "大于 ":
                    std::cout << "等于 ";
    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" 等于 "ONE"
"two" 大于 "four"
"three" 小于 "two"

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用范围 发布行为 正确行为
LWG 3410 C++20 要求进行迭代器间的多余比较 移除此要求

参见

若一个范围按字典序小于另一个范围则返回 true
(函数模板)
实现 x <=> y 的约束函数对象
(类)
若一个范围按字典序小于另一个范围则返回 true
(算法函数对象)