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& ,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 |
类型要求 | ||
-InputIt1 必须满足老式输入迭代器(LegacyInputIterator) 。 | ||
-InputIt2 必须满足老式输入迭代器(LegacyInputIterator) 。 | ||
-ForwardIt1 必须满足老式向前迭代器(LegacyForwardIterator) 。 | ||
-ForwardIt2 必须满足老式向前迭代器(LegacyForwardIterator) 。 | ||
-BinaryPred 必须满足二元谓词(BinaryPredicate) 。 |
[编辑]返回值
拥有指向首对两个不相等元素的迭代器的 std::pair。
如果抵达了 last1,那么返回的迭代器对中的第二个迭代器是 first2 后的第 std::distance(first1, last1) 个迭代器。
对于重载 (5-8),如果抵达了 last2,那么返回的迭代器对中的第一个迭代器是 first1 后的第 std::distance(first2, last2) 个迭代器。
[编辑]复杂度
给定 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; returnstd::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; returnstd::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; returnstd::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; returnstd::make_pair(first1, first2);} |
[编辑]示例
此程序确定同时在给定字符串起始与在其结尾按逆序同时找到的最长子串(可能重叠)。
#include <algorithm>#include <iostream>#include <string> std::string mirror_ends(conststd::string& in){returnstd::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) | 查找两个范围的首个不同之处 (算法函数对象) |