在线文档教程
C++
算法 | Algorithm

std::mismatch

STD:错配

Defined in header
template< class InputIt1, class InputIt2 > std::pair<InputIt1,InputIt2> mismatch( InputIt1 first1, InputIt1 last1, InputIt2 first2 (1)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > std::pair<ForwardIt1,ForwardIt2> mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 (2)(since C++17)
template< class InputIt1, class InputIt2, class BinaryPredicate > std::pair<InputIt1,InputIt2> mismatch( InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p (3)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate > std::pair<ForwardIt1,ForwardIt2> mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, BinaryPredicate p (4)(since C++17)
template< class InputIt1, class InputIt2 > std::pair<InputIt1,InputIt2> mismatch( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 (5)(since C++14)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > std::pair<ForwardIt1,ForwardIt2> mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2 (6)(since C++17)
template< class InputIt1, class InputIt2, class BinaryPredicate > std::pair<InputIt1,InputIt2> mismatch( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPredicate p (7)(since C++14)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate > std::pair<ForwardIt1,ForwardIt2> mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2, BinaryPredicate p (8)(since C++17)

从两个范围返回第一个不匹配的元素对:一个由[first1, last1)另一个由[first2,last2).如果last2未提供%28重载%281-4%29%29,它表示first2 + (last1 - first1)...

用1,5%29种元素进行比较operator==...

使用给定的二进制谓词对3,7%29元素进行比较p...

2,4,6,8%29等于%281,3,5,7%29,但根据policy。此重载只参与以下情况下的过载解决方案:std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first1, last1-the first range of the elements
first2, last2-the second range of the elements
policy-the execution policy to use. See execution policy for details.
p-binary predicate which returns ​true if the elements should be treated as equal. The signature of the predicate function should be equivalent to the following: bool pred(const Type1 &a, const Type2 &b The signature does not need to have const &, but the function must not modify the objects passed to it. The types Type1 and Type2 must be such that objects of types InputIt1 and InputIt2 can be dereferenced and then implicitly converted to Type1 and Type2 respectively. ​

类型要求

-InputIt 1必须符合InputIterator的要求。

-InputIt 2必须符合InputIterator的要求。

-前进1必须符合先行者的要求。

-前进2必须符合先行者的要求。

-二数预测必须符合二数预测的要求。

返回值

std::pair三个非等效元素的迭代器。

If no mismatches are found when the comparison reaches last1, the pair holds last1 and the corresponding iterator from the second range. The behavior is undefined if the second range is shorter than the first range.(until C++14)
If no mismatches are found when the comparison reaches last1 or last2, whichever happens first, the pair holds the end iterator and the corresponding iterator from the other range.(since C++14)

复杂性

1-4%29last1---first1的应用operator==或谓词p

5-8%29,最多为%28last1---first1,,,last2---first229项申请operator==或谓词p...

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。

  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

第一版

*。

模板<class InputIt 1,class InputIt 2>std::PING<InputIt 1,InputIt 2>失配%28 InputIt1first 1,InputIt1last1,InputIt2头2%29{,而%28first 1%21=LST 1&&%2A首1==%2A前2%29{++first 1,++first 2;}返回std::make[医]对%28first 1,前2%29;}

第二版

模板<类InputIt 1,类InputIt 2,类BinaryPredicate>std::PING<InputIt 1,InputIt 2>失配%28InputIt1first 1,InputIt1last1,InputIt2 First 2,BinaryPredicate p%29{而%28first 1%21=last1&&p%28%%2A首先,%2A前2%29%29{++first 1,++first 2;}返回std::make[医]对%28first 1,前2%29;}

第三版

模板<class InputIt 1,class InputIt 2>std::对<InputIt 1,InputIt 2,InputIt 2>失配%28 InputIt1first 1,InputIt1last1,InputIt2first 2,InputIt2last2%29{而%28first 1%21=last1&&prep 2%21=last2&&&%2A首1==%2A前2%29{++first 1,++first 2;}返回std::make[医]对%28first 1,前2%29;}

第四版

模板<类InputIt 1,类InputIt 2,类BinaryPredicate>std::PING<InputIt 1,InputIt 2>失配%28InputIt1first 1,InputIt1last1,InputIt2First 2,InputIt2last2,BinaryPredicate p%29{,而%28first 1%21=last1&&first 2%21=last2&p%28%2A首先,%2A前2%29%29{++first 1,++first 2;}返回std::make[医]对%28first 1,前2%29;}

这个程序确定在给定字符串的开头和它的末尾同时找到的最长子字符串,顺序为%28,可能是重叠的%29。

二次

#include <iostream> #include <string> #include <algorithm> 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

二次

另见

equaldetermines if two sets of elements are the same (function template)
findfind_iffind_if_not (C++11)finds the first element satisfying specific criteria (function template)
lexicographical_comparereturns true if one range is lexicographically less than another (function template)
searchsearches for a range of elements (function template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/Algorithm/失配