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 ,则返回的迭代器对中的第二个迭代器是 std:: distance ( first1, last1 ) th 个位于 first2 之后的迭代器。
对于重载 (5-8) ,若 last2 被抵达,则该对中的首个迭代器为 std:: distance ( first2, last2 ) th 个位于 first1 之后的迭代器。
复杂度
给定 N 1 为 std:: distance ( first1, last1 ) 且 N 2 为 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)
|
查找两个范围首次出现差异的位置
(算法函数对象) |