std::mismatch
定義於標頭檔案 <algorithm> |
||
template< class InputIt1, class InputIt2 > std::pair<InputIt1, InputIt2> |
(1) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > std::pair<ForwardIt1, ForwardIt2> |
(2) | (C++17 起) |
template< class InputIt1, class InputIt2, class BinaryPred > std::pair<InputIt1, InputIt2> |
(3) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPred > |
(4) | (C++17 起) |
template< class InputIt1, class InputIt2 > std::pair<InputIt1, InputIt2> |
(5) | (C++14 起) (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > std::pair<ForwardIt1, ForwardIt2> |
(6) | (C++17 起) |
template< class InputIt1, class InputIt2, class BinaryPred > std::pair<InputIt1, InputIt2> |
(7) | (C++14 起) (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPred > |
(8) | (C++17 起) |
返回一個迭代器對,指向來自 [
first1,
last1)
和一個從 first2 開始的範圍中第一個不匹配的元素。
- 對於過載 (1-4),第二個範圍有 std::distance(first1, last1) 個元素。
- 對於過載 (5-8),第二個範圍是
[
first2,
last2)
。
- 如果 std::distance(first1, last1) 和 std::distance(first2, last2) 不同,則當到達 last1 或 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 | - | 要使用的 執行策略 |
p | - | 二元謂詞,如果元素應被視為相等,則返回 true。 謂詞函式的簽名應等效於以下內容: bool pred(const Type1 &a, const Type2 &b); 雖然簽名不需要包含 const &,但函式不得修改傳遞給它的物件,並且必須能夠接受 |
型別要求 | ||
-InputIt1 必須滿足 LegacyInputIterator 的要求。 | ||
-InputIt2 必須滿足 LegacyInputIterator 的要求。 | ||
-ForwardIt1 必須滿足 LegacyForwardIterator 的要求。 | ||
-ForwardIt2 必須滿足 LegacyForwardIterator 的要求。 | ||
-BinaryPred 必須滿足 BinaryPredicate 的要求。 |
[編輯] 返回值
std::pair,包含指向前兩個不相等元素的迭代器。
如果到達 last1,則對中的第二個迭代器是 std::distance(first1, last1) 個迭代器,位於 first2 之後。
對於過載 (5-8),如果到達 last2,則對中的第一個迭代器是 std::distance(first2, last2) 個迭代器,位於 first1 之後。
[編輯] 複雜度
給定 N1 為 std::distance(first1, last1) 且 N2 為 std::distance(first2, last2)
[編輯] 異常
帶有模板引數 ExecutionPolicy
的過載按如下方式報告錯誤
- 如果作為演算法一部分呼叫的函式執行丟擲異常,並且
ExecutionPolicy
是 標準策略 之一,則呼叫 std::terminate。對於任何其他ExecutionPolicy
,行為是實現定義的。 - 如果演算法未能分配記憶體,則丟擲 std::bad_alloc。
[編輯] 可能的實現
mismatch (1) |
---|
template<class InputIt1, class InputIt2> std::pair<InputIt1, InputIt2> mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2) { while (first1 != last1 && *first1 == *first2) ++first1, ++first2; return std::make_pair(first1, first2); } |
mismatch (3) |
template<class InputIt1, class InputIt2, class BinaryPred> std::pair<InputIt1, InputIt2> mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPred p) { while (first1 != last1 && p(*first1, *first2)) ++first1, ++first2; return std::make_pair(first1, first2); } |
mismatch (5) |
template<class InputIt1, class InputIt2> std::pair<InputIt1, InputIt2> mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) { while (first1 != last1 && first2 != last2 && *first1 == *first2) ++first1, ++first2; return std::make_pair(first1, first2); } |
mismatch (7) |
template<class InputIt1, class InputIt2, class BinaryPred> std::pair<InputIt1, InputIt2> mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPred p) { while (first1 != last1 && first2 != last2 && p(*first1, *first2)) ++first1, ++first2; return std::make_pair(first1, first2); } |
[編輯] 示例
此程式確定給定字串開頭和結尾處(反向,可能重疊)同時找到的最長子字串。
#include <algorithm> #include <iostream> #include <string> std::string mirror_ends(const std::string& in) { return std::string(in.begin(), std::mismatch(in.begin(), in.end(), in.rbegin()).first); } int main() { std::cout << mirror_ends("abXYZba") << '\n' << mirror_ends("abca") << '\n' << mirror_ends("aba") << '\n'; }
輸出
ab a aba
[編輯] 參見
判斷兩組元素是否相同 (函式模板) | |
(C++11) |
尋找第一個滿足特定條件的元素 (函式模板) |
如果一個範圍在字典上小於另一個範圍,則返回 true (函式模板) | |
搜尋一個範圍的元素首次出現的位置 (函式模板) | |
(C++20) |
尋找兩個範圍開始不同的第一個位置 (演算法函式物件) |