跳到主要内容

std::mismatch() 算法

// (1)
template< class InputIt1, class InputIt2 >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2 );

// (2)
template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, BinaryPredicate p );

// (3)
template< class InputIt1, class InputIt2 >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2 );

// (4)
template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, BinaryPredicate p );

// (5)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2 );

// (6)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class BinaryPredicate >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, BinaryPredicate p );

// (7)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2 );

// (8)
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 )

返回两个范围中第一个不匹配的元素对

  • 一个由 [first1; last1) 定义。
  • 另一个由 [first2; last2) 定义。

对于 (1, 2, 5, 6),如果未提供 last2,则表示 first2 + (last1 - first1)

  • (1, 3) 元素使用 operator== 进行比较。

  • (2, 4) 元素使用给定的二元谓词 p 进行比较。

  • (5 - 8)(1 - 4) 相同,但根据策略执行。

    重载决议

    这些重载只有在 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 fun(const Type1& a, const Type2& b);
  • 签名不需要包含 const&
  • 函数不得修改传递给它的对象
  • 必须接受类型为(可能为 const)TypeType2 的所有值,无论值类别如何(因此不允许使用 Type1&也不允许使用 Type1,除非对于 Type1,移动等效于复制 (自 C++11 起)
  • 类型 Type1Type2 必须是这样的:类型为 InputIt1InputIt2 的对象可以被解引用然后隐式转换为它们。

类型要求

InputIt1
InputIt2
LegacyInputIterator
ForwardIt1
ForwardIt2
LegacyForwardIterator
BinaryPredicateBinaryPredicate

返回值

一个包含指向前两个不相等元素的迭代器的std::pair

如果在比较达到 last1last2(以先发生者为准)时未发现不匹配,则该对包含结束迭代器和来自另一个范围的相应迭代器。

复杂度

  • (1, 2, 5, 6) 最多 last1 - first1operator== 或谓词 p 的应用。
  • (3, 4, 7, 8) 精确地 std::distance(first1, last1)unary_op 的应用。

异常

带有模板参数 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 (2)
template<class InputIt1, class InputIt2, class BinaryPredicate>
std::pair<InputIt1, InputIt2>
mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p)
{
while (first1 != last1 && p(*first1, *first2))
++first1, ++first2;

return std::make_pair(first1, first2);
}
mismatch (3)
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 (4)
template<class InputIt1, class InputIt2, class BinaryPredicate>
std::pair<InputIt1, InputIt2>
mismatch(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, BinaryPredicate p)
{
while (first1 != last1 && first2 != last2 && p(*first1, *first2))
++first1, ++first2;

return std::make_pair(first1, first2);
}

示例

此程序确定给定字符串开头和结尾(反向)同时存在的最长子字符串(可能重叠)。

以下代码使用 mismatch 通过 std::toupper 函数将字符串原地转换为大写,然后将每个字符与其序数值进行不匹配

Main.cpp
#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
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了更改。单击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。

std::mismatch() 算法

// (1)
template< class InputIt1, class InputIt2 >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2 );

// (2)
template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, BinaryPredicate p );

// (3)
template< class InputIt1, class InputIt2 >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2 );

// (4)
template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr std::pair<InputIt1, InputIt2>
mismatch( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, BinaryPredicate p );

// (5)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2 );

// (6)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class BinaryPredicate >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, BinaryPredicate p );

// (7)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
std::pair<ForwardIt1, ForwardIt2>
mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2 );

// (8)
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 )

返回两个范围中第一个不匹配的元素对

  • 一个由 [first1; last1) 定义。
  • 另一个由 [first2; last2) 定义。

对于 (1, 2, 5, 6),如果未提供 last2,则表示 first2 + (last1 - first1)

  • (1, 3) 元素使用 operator== 进行比较。

  • (2, 4) 元素使用给定的二元谓词 p 进行比较。

  • (5 - 8)(1 - 4) 相同,但根据策略执行。

    重载决议

    这些重载只有在 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 fun(const Type1& a, const Type2& b);
  • 签名不需要包含 const&
  • 函数不得修改传递给它的对象
  • 必须接受类型为(可能为 const)TypeType2 的所有值,无论值类别如何(因此不允许使用 Type1&也不允许使用 Type1,除非对于 Type1,移动等效于复制 (自 C++11 起)
  • 类型 Type1Type2 必须是这样的:类型为 InputIt1InputIt2 的对象可以被解引用然后隐式转换为它们。

类型要求

InputIt1
InputIt2
LegacyInputIterator
ForwardIt1
ForwardIt2
LegacyForwardIterator
BinaryPredicateBinaryPredicate

返回值

一个包含指向前两个不相等元素的迭代器的std::pair

如果在比较达到 last1last2(以先发生者为准)时未发现不匹配,则该对包含结束迭代器和来自另一个范围的相应迭代器。

复杂度

  • (1, 2, 5, 6) 最多 last1 - first1operator== 或谓词 p 的应用。
  • (3, 4, 7, 8) 精确地 std::distance(first1, last1)unary_op 的应用。

异常

带有模板参数 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 (2)
template<class InputIt1, class InputIt2, class BinaryPredicate>
std::pair<InputIt1, InputIt2>
mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p)
{
while (first1 != last1 && p(*first1, *first2))
++first1, ++first2;

return std::make_pair(first1, first2);
}
mismatch (3)
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 (4)
template<class InputIt1, class InputIt2, class BinaryPredicate>
std::pair<InputIt1, InputIt2>
mismatch(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, BinaryPredicate p)
{
while (first1 != last1 && first2 != last2 && p(*first1, *first2))
++first1, ++first2;

return std::make_pair(first1, first2);
}

示例

此程序确定给定字符串开头和结尾(反向)同时存在的最长子字符串(可能重叠)。

以下代码使用 mismatch 通过 std::toupper 函数将字符串原地转换为大写,然后将每个字符与其序数值进行不匹配

Main.cpp
#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
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了更改。单击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。