Namespaces
Variants

std::mismatch

来自 zh.cppreference.net
 
 
算法库
受约束算法与范围上的算法 (C++20)
受约束算法,例如 ranges::copy、ranges::sort、...
排序与相关操作
划分操作
(C++11)    

排序操作
二分搜索操作
(在已划分的范围上)
集合操作(在已排序范围上)
合并操作(在已排序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作


 
定义于头文件 <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 时停止。
1,5) 使用 operator== 比较元素。
3,7) 使用给定的二元谓词 p 比较元素。
2,4,6,8) 同 (1,3,5,7),但按 policy 执行。
这些重载仅在下述表达式求值为 true 时参与重载决议:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>

(C++20 前)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>

(C++20 起)

参数

first1, last1 - 定义第一范围元素对进行比较的迭代器对
first2, last2 - 定义第二范围元素对进行比较的迭代器对
policy - 要使用的执行策略
p - 二元谓词,如果元素应被视为相等则返回 ​true。

谓词函数的签名应等价于以下:

bool pred(const Type1 &a, const Type2 &b);

虽然签名不需要有 const &,但函数必须不修改传递给它的对象,并且必须能够接受(可能为 const 的)类型 Type1 和 Type2 的所有值,无论值类别如何(因此,不允许 Type1 &,也不允许 Type1,除非对于 Type1 移动等价于复制(C++11 起))。
类型 Type1 和 Type2 必须使得类型 InputIt1 和 InputIt2 的对象可以被解引用,然后隐式转换为 Type1 和 Type2。 ​

类型要求
-
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):

1,2) 最多 N1 次使用 operator== 的比较。
3,4) 最多 N1 次应用谓词 p。
5,6) 最多 min(N1,N2) 次使用 operator== 的比较。
7,8) 最多 min(N1,N2) 次应用谓词 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

参阅

判断两个元素集合是否相同
(函数模板 & 算法函数对象)
查找满足特定条件的第一个元素
(函数模板 & 算法函数对象)
按字典序比较两个范围
(函数模板 & 算法函数对象)
搜索一个元素范围的首次出现
(函数模板 & 算法函数对象)
查找两个范围首次出现差异的位置
(算法函数对象)