std::mismatch
定义于头文件 <algorithm> |
||
template< class InputIt1, class InputIt2 > std::pair<InputIt1, InputIt2> |
(1) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > std::pair<ForwardIt1, ForwardIt2> |
(2) | (since C++17) |
template< class InputIt1, class InputIt2, class BinaryPred > std::pair<InputIt1, InputIt2> |
(3) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPred > |
(4) | (since C++17) |
template< class InputIt1, class InputIt2 > std::pair<InputIt1, InputIt2> |
(5) | (since C++14) (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > std::pair<ForwardIt1, ForwardIt2> |
(6) | (since C++17) |
template< class InputIt1, class InputIt2, class BinaryPred > std::pair<InputIt1, InputIt2> |
(7) | (since C++14) (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPred > |
(8) | (since 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 &,但函数不能修改传递给它的对象,并且必须能够接受类型 |
类型要求 | ||
-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)
[编辑] 异常
具有名为 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) |
查找两个范围不同的第一个位置 (算法函数对象) |