std::lexicographical_compare_three_way

来自cppreference.com
< cpp‎ | algorithm
 
 
算法库
受约束算法及范围上的算法(C++20)
包含算法例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)
排序和相关操作
划分操作
排序操作
二分搜索操作(在已划分范围上)
集合操作(在有序范围上)
归并操作(在有序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
lexicographical_compare_three_way
(C++20)
排列操作
C 库

数值运算
(C++11)                       
在未初始化内存上的操作
 
在标头 <algorithm> 定义
template<class InputIt1, class InputIt2, class Cmp >

constexprauto 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 >

constexprauto 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(N1,N2)comp
2) 最多应用 min(N1,N2)std::compare_three_way()

[编辑]可能的实现

template<class I1, class I2, class Cmp>constexprauto 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>   usingnamespace std::literals;   void show_result(std::string_view s1, std::string_view s2, std::strong_ordering o){std::cout<< quoted(s1)<<" ";(o <0)?std::cout<<"小于 ":(o >0)?std::cout<<"大于 ":std::cout<<"等于 ";std::cout<< quoted(s2)<<'\n';}   std::strong_ordering cmp_icase(unsignedchar x, unsignedchar y){returnstd::toupper(x)<=>std::toupper(y);};   int main(){for(constauto&[s1, s2]:{std::pair{"one"sv, "ONE"sv}, {"two"sv, "four"sv}, {"three"sv, "two"sv}}){constauto 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
(算法函数对象)[编辑]
close