std::lexicographical_compare
定義於標頭檔案 <algorithm> |
||
template< class InputIt1, class InputIt2 > bool lexicographical_compare( InputIt1 first1, InputIt1 last1, |
(1) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > |
(2) | (C++17 起) |
template< class InputIt1, class InputIt2, class Compare > bool lexicographical_compare( InputIt1 first1, InputIt1 last1, |
(3) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class Compare > |
(4) | (C++17 起) |
檢查第一個範圍 [
first1,
last1)
是否按字典序小於第二個範圍 [
first2,
last2)
。
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) |
型別要求 | ||
-InputIt1, InputIt2 必須滿足 LegacyInputIterator 的要求。 | ||
-ForwardIt1, ForwardIt2 必須滿足 LegacyForwardIterator 的要求。 | ||
-Compare 必須滿足 Compare 的要求。 |
[編輯] 返回值
如果第一個範圍在字典序上小於第二個範圍,則為 true,否則為 false。
[編輯] 複雜度
給定 N1 作為 std::distance(first1, last1),和 N2 作為 std::distance(first2, last2)
[編輯] 異常
帶有模板引數 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 (演算法函式物件) |