命名空间
变体
操作

std::mismatch

来自 cppreference.cn
< cpp‎ | 算法
 
 
算法库
有约束算法与针对范围的算法 (C++20)
有约束的算法,例如 ranges::copyranges::sort 等……
执行策略 (C++17)
排序及相关操作
划分操作
排序操作
二分搜索操作
(于已划分范围上)
集合操作(于已排序范围上)
归并操作(于已排序范围上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库
数值操作
未初始化内存上的操作
 
定义于头文件 <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 起)

返回一个迭代器对,指向来自 [first1last1) 和一个从 first2 开始的范围中第一个不匹配的元素。

  • 对于重载 (1-4),第二个范围有 std::distance(first1, last1) 个元素。
  • 对于重载 (5-8),第二个范围是 [first2last2)
1,5) 元素使用 operator== 进行比较。
3,7) 元素使用给定的二元谓词 p 进行比较。
2,4,6,8)(1,3,5,7) 相同,但根据 policy 执行。
仅当满足所有以下条件时,这些重载才参与重载决议

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 &,但函数不得修改传递给它的对象,并且必须能够接受 Type1Type2 类型(可能是 const)的所有值,无论 值类别 (因此,不允许使用 Type1 & ,也不允许使用 Type1,除非对于 Type1 移动等同于复制(C++11 起))。
类型 Type1Type2 必须使得类型为 InputIt1InputIt2 的对象可以被解引用,然后隐式转换为 Type1Type2。​

类型要求
-
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 之后。

[编辑] 复杂度

给定 N1std::distance(first1, last1)N2std::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

[编辑] 参见

判断两组元素是否相同
(函数模板) [编辑]
寻找第一个满足特定条件的元素
(函数模板) [编辑]
如果一个范围在字典上小于另一个范围,则返回 true
(函数模板) [编辑]
搜索一个范围的元素首次出现的位置
(函数模板) [编辑]
寻找两个范围开始不同的第一个位置
(算法函数对象)[编辑]