跳到主要内容

std::adjacent_find() 算法

// (1)
template< class ForwardIt >
constexpr ForwardIt adjacent_find( ForwardIt first, ForwardIt last );

// (2)
template< class ForwardIt, class BinaryPredicate >
constexpr ForwardIt adjacent_find( ForwardIt first, ForwardIt last, BinaryPredicate p );

// (3)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt adjacent_find( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last );

// (4)
template< class ExecutionPolicy, class ForwardIt, class BinaryPredicate >
ForwardIt adjacent_find( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, BinaryPredicate p );

在范围 [first; last) 中搜索两个连续相等的元素。

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

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

  • (3 - 4)(1 - 2) 相同,但根据 policy 执行。

    重载决议

    这些重载只有在 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (C++20 之前) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (C++20 起)true 时才参与重载决议。

参数

first
last

要检查的元素范围。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

p

二元谓词,如果元素应被视为相等,则返回 true

函数的签名应与以下内容等效

bool fun(const Type1& a, const Type2& b);
  • 签名不需要包含 const&
  • 函数不得修改传递给它的对象。
  • 必须接受类型为 (可能 const) TypeType2 的所有值,无论值类别如何(因此不允许 Type1&也不允许 Type1,除非对于 Type1 移动等同于复制 (C++11 起)
  • Type1Type2 的类型必须使得 ForwardIt 类型的对象可以被解引用然后隐式转换为它们。

类型要求

ForwardItLegacyForwardIterator

返回值

指向第一对相同元素的第一个迭代器,即对于 (1, 3),是第一个迭代器 it,使得 *it == *(it + 1);对于 (2, 4),是第一个迭代器 it,使得 p(*it, *(it + 1)) != false

如果未找到此类元素,则返回 last

复杂度

  • (1, 3) 谓词的调用次数恰好为 std::min((result - first) + 1, (last - first) - 1),其中 result 是返回值。
  • (2, 4) 对应谓词的调用次数为 O(last - first)

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数抛出异常,并且 ExecutionPolicy标准策略之一,则调用std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

可能的实现

adjacent_find (1)
template<class ForwardIt>
ForwardIt adjacent_find(ForwardIt first, ForwardIt last)
{
if (first == last)
return last;

ForwardIt next = first;
++next;

for (; next != last; ++next, ++first)
if (*first == *next)
return first;

return last;
}
adjacent_find (2)
template<class ForwardIt, class BinaryPredicate>
ForwardIt adjacent_find(ForwardIt first, ForwardIt last, BinaryPredicate p)
{
if (first == last)
return last;

ForwardIt next = first;
++next;

for (; next != last; ++next, ++first)
if (p(*first, *next))
return first;

return last;
}

示例

Main.cpp
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

int main()
{
std::vector<int> v1 {0, 1, 2, 3, 40, 40, 41, 41, 5};

auto i1 = std::adjacent_find(v1.begin(), v1.end());

if (i1 == v1.end())
std::cout << "No matching adjacent elements\n";
else
std::cout << "The first adjacent pair of equal elements is at "
<< std::distance(v1.begin(), i1) << ", *i1 = "
<< *i1 << '\n';

auto i2 = std::adjacent_find(v1.begin(), v1.end(), std::greater<int>());
if (i2 == v1.end())
std::cout << "The entire vector is sorted in ascending order\n";
else
std::cout << "The last element in the non-decreasing subsequence is at "
<< std::distance(v1.begin(), i2) << ", *i2 = " << *i2 << '\n';
}
输出
The first adjacent pair of equal elements is at 4, *i1 = 40
The last element in the non-decreasing subsequence is at 7, *i2 = 41
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看此文档所做的所有更改。
悬停查看原始许可证。

std::adjacent_find() 算法

// (1)
template< class ForwardIt >
constexpr ForwardIt adjacent_find( ForwardIt first, ForwardIt last );

// (2)
template< class ForwardIt, class BinaryPredicate >
constexpr ForwardIt adjacent_find( ForwardIt first, ForwardIt last, BinaryPredicate p );

// (3)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt adjacent_find( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last );

// (4)
template< class ExecutionPolicy, class ForwardIt, class BinaryPredicate >
ForwardIt adjacent_find( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, BinaryPredicate p );

在范围 [first; last) 中搜索两个连续相等的元素。

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

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

  • (3 - 4)(1 - 2) 相同,但根据 policy 执行。

    重载决议

    这些重载只有在 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (C++20 之前) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (C++20 起)true 时才参与重载决议。

参数

first
last

要检查的元素范围。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

p

二元谓词,如果元素应被视为相等,则返回 true

函数的签名应与以下内容等效

bool fun(const Type1& a, const Type2& b);
  • 签名不需要包含 const&
  • 函数不得修改传递给它的对象。
  • 必须接受类型为 (可能 const) TypeType2 的所有值,无论值类别如何(因此不允许 Type1&也不允许 Type1,除非对于 Type1 移动等同于复制 (C++11 起)
  • Type1Type2 的类型必须使得 ForwardIt 类型的对象可以被解引用然后隐式转换为它们。

类型要求

ForwardItLegacyForwardIterator

返回值

指向第一对相同元素的第一个迭代器,即对于 (1, 3),是第一个迭代器 it,使得 *it == *(it + 1);对于 (2, 4),是第一个迭代器 it,使得 p(*it, *(it + 1)) != false

如果未找到此类元素,则返回 last

复杂度

  • (1, 3) 谓词的调用次数恰好为 std::min((result - first) + 1, (last - first) - 1),其中 result 是返回值。
  • (2, 4) 对应谓词的调用次数为 O(last - first)

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数抛出异常,并且 ExecutionPolicy标准策略之一,则调用std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

可能的实现

adjacent_find (1)
template<class ForwardIt>
ForwardIt adjacent_find(ForwardIt first, ForwardIt last)
{
if (first == last)
return last;

ForwardIt next = first;
++next;

for (; next != last; ++next, ++first)
if (*first == *next)
return first;

return last;
}
adjacent_find (2)
template<class ForwardIt, class BinaryPredicate>
ForwardIt adjacent_find(ForwardIt first, ForwardIt last, BinaryPredicate p)
{
if (first == last)
return last;

ForwardIt next = first;
++next;

for (; next != last; ++next, ++first)
if (p(*first, *next))
return first;

return last;
}

示例

Main.cpp
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

int main()
{
std::vector<int> v1 {0, 1, 2, 3, 40, 40, 41, 41, 5};

auto i1 = std::adjacent_find(v1.begin(), v1.end());

if (i1 == v1.end())
std::cout << "No matching adjacent elements\n";
else
std::cout << "The first adjacent pair of equal elements is at "
<< std::distance(v1.begin(), i1) << ", *i1 = "
<< *i1 << '\n';

auto i2 = std::adjacent_find(v1.begin(), v1.end(), std::greater<int>());
if (i2 == v1.end())
std::cout << "The entire vector is sorted in ascending order\n";
else
std::cout << "The last element in the non-decreasing subsequence is at "
<< std::distance(v1.begin(), i2) << ", *i2 = " << *i2 << '\n';
}
输出
The first adjacent pair of equal elements is at 4, *i1 = 40
The last element in the non-decreasing subsequence is at 7, *i2 = 41
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看此文档所做的所有更改。
悬停查看原始许可证。