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 执行。|
|
(C++20 前) |
|
|
(C++20 起) |
参数
| first1, last1 | - | 要比较的第一元素范围的迭代器对 |
| first2, last2 | - | 要比较的第二元素范围的迭代器对 |
| policy | - | 所用的执行策略 |
| p | - | 若元素应被当做相等则返回 true 的二元谓词谓词函数的签名应等价于如下:
虽然签名不必有 |
| 类型要求 | ||
-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):
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++11) |
查找首个满足特定条件的元素 (函数模板) |
当一个范围字典序小于另一个时返回 true (函数模板) | |
| 搜索元素范围的首次出现 (函数模板) | |
(C++20) |
查找两个范围的首个不同之处 (算法函数对象) |