名稱空間
變體
操作

std::lexicographical_compare

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
lexicographical_compare
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
定義於標頭檔案 <algorithm>
template< class InputIt1, class InputIt2 >

bool lexicographical_compare( InputIt1 first1, InputIt1 last1,

                              InputIt2 first2, InputIt2 last2 );
(1) (C++20 起為 constexpr)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2 >
bool lexicographical_compare( ExecutionPolicy&& policy,
                              ForwardIt1 first1, ForwardIt1 last1,

                              ForwardIt2 first2, ForwardIt2 last2 );
(2) (C++17 起)
template< class InputIt1, class InputIt2, class Compare >

bool lexicographical_compare( InputIt1 first1, InputIt1 last1,
                              InputIt2 first2, InputIt2 last2,

                              Compare comp );
(3) (C++20 起為 constexpr)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2, class Compare >
bool lexicographical_compare( ExecutionPolicy&& policy,
                              ForwardIt1 first1, ForwardIt1 last1,
                              ForwardIt2 first2, ForwardIt2 last2,

                              Compare comp );
(4) (C++17 起)

檢查第一個範圍 [first1last1) 是否按字典序小於第二個範圍 [first2last2)

1) 元素使用 operator< 進行比較。
3) 元素使用給定的二元比較函式 comp 進行比較。
2,4)(1,3) 相同,但根據 policy 執行。這些過載僅在滿足以下所有條件時才參與過載決議:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>true

(C++20 前)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>true

(C++20 起)

字典序比較是一種具有以下屬性的操作:

  • 兩個範圍逐元素進行比較。
  • 第一個不匹配的元素定義了哪個範圍在字典序上小於大於另一個。
  • 如果一個範圍是另一個範圍的字首,則較短的範圍在字典序上小於另一個。
  • 如果兩個範圍具有等效元素且長度相同,則這兩個範圍在字典序上相等
  • 空範圍在字典序上小於任何非空範圍。
  • 兩個空範圍在字典序上相等

目錄

[編輯] 引數

first1, last1 - 定義要檢查的第一個元素範圍的迭代器對。
first2, last2 - 定義要檢查的第二個元素範圍的迭代器對。
policy - 要使用的 執行策略
comp - 比較函式物件(即滿足 Compare 要求的物件),如果第一個引數小於第二個引數,則返回 true

比較函式的簽名應等效於以下內容

bool cmp(const Type1& a, const Type2& b);

雖然簽名不需要包含 const&,但函式不得修改傳遞給它的物件,並且必須能夠接受所有型別為(可能為 const)Type1Type2 的值,無論其值類別如何(因此,不允許使用 Type1&,也不允許使用 Type1,除非對於 Type1,移動等同於複製(自 C++11 起))。
型別 Type1Type2 必須使得型別為 InputIt1InputIt2 的物件可以被解引用,然後隱式轉換為 Type1Type2

型別要求
-
InputIt1, InputIt2 必須滿足 LegacyInputIterator 的要求。
-
ForwardIt1, ForwardIt2 必須滿足 LegacyForwardIterator 的要求。
-
Compare 必須滿足 Compare 的要求。

[編輯] 返回值

如果第一個範圍在字典序上小於第二個範圍,則為 true,否則為 false

[編輯] 複雜度

給定 N1 作為 std::distance(first1, last1),和 N2 作為 std::distance(first2, last2)

1,2) 最多 2min(1,N2) 次使用 operator< 的比較。
3,4) 最多 2min(N1,N2) 次比較函式 comp 的應用。

[編輯] 異常

帶有模板引數 ExecutionPolicy 的過載按如下方式報告錯誤

  • 如果作為演算法一部分呼叫的函式執行丟擲異常,並且 ExecutionPolicy標準策略之一,則呼叫 std::terminate。對於任何其他 ExecutionPolicy,行為是實現定義的。
  • 如果演算法未能分配記憶體,則丟擲 std::bad_alloc

[編輯] 可能的實現

lexicographical_compare (1)
template<class InputIt1, class InputIt2>
bool lexicographical_compare(InputIt1 first1, InputIt1 last1,
                             InputIt2 first2, InputIt2 last2)
{
    for (; (first1 != last1) && (first2 != last2); ++first1, (void) ++first2)
    {
        if (*first1 < *first2)
            return true;
        if (*first2 < *first1)
            return false;
    }
 
    return (first1 == last1) && (first2 != last2);
}
lexicographical_compare (3)
template<class InputIt1, class InputIt2, class Compare>
bool lexicographical_compare(InputIt1 first1, InputIt1 last1,
                             InputIt2 first2, InputIt2 last2, Compare comp)
{
    for (; (first1 != last1) && (first2 != last2); ++first1, (void) ++first2)
    {
        if (comp(*first1, *first2))
            return true;
        if (comp(*first2, *first1))
            return false;
    }
 
    return (first1 == last1) && (first2 != last2);
}

[編輯] 示例

#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
 
void print(const std::vector<char>& v, auto suffix)
{
    for (char c : v)
        std::cout << c << ' ';
    std::cout << suffix;
}
 
int main()
{
    std::vector<char> v1{'a', 'b', 'c', 'd'};
    std::vector<char> v2{'a', 'b', 'c', 'd'};
 
    for (std::mt19937 g{std::random_device{}()};
         !std::lexicographical_compare(v1.begin(), v1.end(),
                                       v2.begin(), v2.end());)
    {
        print(v1, ">= ");
        print(v2, '\n');
 
        std::shuffle(v1.begin(), v1.end(), g);
        std::shuffle(v2.begin(), v2.end(), g);
    }
 
    print(v1, "<  ");
    print(v2, '\n');
}

可能的輸出

a b c d >= a b c d 
d a b c >= c b d a 
b d a c >= a d c b 
a c d b <  c d a b

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 142 C++98 允許最多 min(N1,N2) 次比較,但這
是不可能的(等價性由 2 次比較確定)
將限制加倍
LWG 1205 C++98 涉及空範圍的字典序比較結果不明確 已明確

[編輯] 另請參閱

判斷兩組元素是否相同
(函式模板) [編輯]
使用三路比較比較兩個範圍
(函式模板) [編輯]
如果一個範圍在字典上小於另一個範圍,則返回 true
(演算法函式物件)[編輯]