Namespaces
Variants

std::lexicographical_compare_three_way

ja.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から)

2つの範囲を辞書順比較し、[first1, last1) と [first2, last2) 三方比較を用いて、最も強い適用可能な比較カテゴリ型の結果を生成します。

1) 最初の同等でない要素のペアの間の順序を、comp 両方の範囲に存在する場合、そうでなければ(一方の範囲が他方の範囲の接頭辞と同等である場合、comp)、両方の範囲の長さの間の順序を返します。
2) 以下と同等: return std::lexicographical_compare_three_way(
first1, last1, first2, last2, std::compare_three_way());

戻り値の型が3つの比較カテゴリ型のいずれでもない場合、プログラムは不正形式です:

引数

first1, last1 - 最初の範囲の要素を調査するためのイテレータのペア
first2, last2 - 2番目の範囲の要素を調査するためのイテレータのペア
comp - 関数オブジェクト
型要件
-
InputIt1, InputIt2 は、LegacyInputIteratorの要件を満たす必要があります。

戻り値

上記で指定された比較カテゴリ型の値。

計算量

与えられた N1 を std::distance(first1, last1) と N2 を std::distance(first2, last2):

1) 最大 min(1,N2)comp 回の
。 2)\(\scriptsize \min(N_1,N_2)\)min(1,N2std::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

イテレータ間の余分な比較が必須だった

関連項目
lexicographical_compare&2つの範囲を辞書順に比較する
アルゴリズム関数オブジェクト)
compare_three_wayx <=> y
(C++20)
2つの範囲を辞書順に比較する
(アルゴリズム関数オブジェクト)