std::mismatch
| 定义于头文件 <algorithm>
|
||
template< class InputIt1, class InputIt2 >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2 );
|
(1) | (自 C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2 );
|
(2) | (C++17 起) |
template< class InputIt1, class InputIt2, class BinaryPred >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, BinaryPred p );
|
(3) | (自 C++20 起为 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class BinaryPred >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, BinaryPred p );
|
(4) | (C++17 起) |
template< class InputIt1, class InputIt2 >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2 );
|
(5) | (C++14 起) (自 C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2 );
|
(6) | (C++17 起) |
template< class InputIt1, class InputIt2, class BinaryPred >
std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, BinaryPred p );
|
(7) | (C++14 起) (自 C++20 起为 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class BinaryPred >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2, BinaryPred p );
|
(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时停止。
- 如果
operator== 比较元素。p 比较元素。policy 执行。true 时参与重载决议:
|
|
(C++20 前) |
|
|
(C++20 起) |
参数
| first1, last1 | - | 定义第一范围元素对进行比较的迭代器对 |
| first2, last2 | - | 定义第二范围元素对进行比较的迭代器对 |
| policy | - | 要使用的执行策略 |
| p | - | 二元谓词,如果元素应被视为相等则返回 true。谓词函数的签名应等价于以下:
虽然签名不需要有 |
| 类型要求 | ||
-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):
operator== 的比较。p。operator== 的比较。p。异常
含有模板参数名为 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++20) |
|
(C++11) |
查找满足特定条件的第一个元素 (函数模板 & 算法函数对象) |
(C++20)(C++20)(C++20) |
|
| 按字典序比较两个范围 (函数模板 & 算法函数对象) | |
| 搜索一个元素范围的首次出现 (函数模板 & 算法函数对象) | |
(C++20) |
|
(C++20) |
查找两个范围首次出现差异的位置 (算法函数对象) |