命名空间
变体
操作

std::lexicographical_compare_three_way

来自 cppreference.cn
< cpp‎ | algorithm
 
 
算法库
约束算法和范围上的算法 (C++20)
约束算法,例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)
排序和相关操作
划分操作
排序操作
二分搜索操作
(在已划分范围上)
集合操作(在已排序范围上)
归并操作(在已排序范围上)
堆操作
最小值/最大值操作
(C++11)
(C++17)
字典序比较操作
lexicographical_compare_three_way
(C++20)
排列操作
C 库
数值操作
未初始化内存上的操作
 
定义于头文件 <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 起)

使用三路比较字典序比较两个范围 [first1last1)[first2last2),并产生最强适用比较类别类型的结果。

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 的要求。

[编辑] 返回值

上面指定的比较类别类型的值。

[编辑] 复杂度

给定 N1std::distance(first1, last1)N2std::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 需要迭代器之间多余的比较 移除此类要求

[编辑] 参见

如果一个范围在字典序上小于另一个范围,则返回 true
(函数模板) [编辑]
实现 x <=> y 的约束函数对象
(类) [编辑]
如果一个范围在字典序上小于另一个范围,则返回 true
(算法函数对象)[编辑]