跳到主要内容

std::ranges::adjacent_find() 算法

// (1)
constexpr I
adjacent_find( I first, S last, Pred pred = {}, Proj proj = {} );

// (2)
constexpr ranges::borrowed_iterator_t<R>
adjacent_find( R&& r, Pred pred = {}, Proj proj = {} );

参数类型是泛型的,并具有以下约束

  • I - std::forward_iterator

  • S - std::sentinel_for<I>

  • Proj - (无)

  • Pred:

    • (1) - indirect_binary_predicate<projected<I, Proj>, projected<I, Proj>>
    • (2) - indirect_binary_predicate<projected<ranges::iterator_t<R>, Proj>, projected<ranges::iterator_t<R>, Proj>>

    (为方便阅读,此处省略了 std:: 命名空间)

  • (2) - R - std::ranges::input_range

对于所有重载,Proj 模板参数的默认类型为 std::identity

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

  • (1) 元素使用 pred 进行比较(在使用投影 proj 进行投影之后)。

  • (2)(1) 相同,但使用 r 作为源范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

本页描述的函数类实体是niebloids

参数

first
last

要检查的元素范围。

r

要检查的元素范围。

pred

应用于投影元素的谓词。

proj

应用于元素的投影。

返回值

指向第一对相同元素的第一个元素的迭代器,即第一个迭代器 `it`,使得 `bool(std::invoke(pred, std::invoke(proj1, *it), std::invoke(proj, *(it + 1))))` 为 `true`。

如果未找到此类元素,则返回等于 last 的迭代器。

复杂度

谓词和投影的精确应用次数为 min((result - first) + 1, (last - first) - 1),其中 result 是返回值。

异常

(无)

可能的实现

adjacent_find(1) 和 adjacent_find(2)
struct adjacent_find_fn
{
template<std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
std::indirect_binary_predicate<
std::projected<I, Proj>,
std::projected<I, Proj>> Pred = ranges::equal_to>
constexpr I operator()(I first, S last, Pred pred = {}, Proj proj = {}) const
{
if (first == last)
return first;
auto next = ranges::next(first);
for (; next != last; ++next, ++first)
if (std::invoke(pred, std::invoke(proj, *first), std::invoke(proj, *next)))
return first;
return next;
}

template<ranges::forward_range R, class Proj = std::identity,
std::indirect_binary_predicate<
std::projected<ranges::iterator_t<R>, Proj>,
std::projected<ranges::iterator_t<R>, Proj>> Pred = ranges::equal_to>
constexpr ranges::borrowed_iterator_t<R>
operator()(R&& r, Pred pred = {}, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
}
};

inline constexpr adjacent_find_fn adjacent_find;

示例

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

int main()
{
const auto v = {0, 1, 2, 3, 40, 40, 41, 41, 5}; /*
^^ ^^ */
namespace ranges = std::ranges;

if (auto it = ranges::adjacent_find(v.begin(), v.end()); it == v.end())
std::cout << "No matching adjacent elements\n";
else
std::cout << "The first adjacent pair of equal elements is at ["
<< ranges::distance(v.begin(), it) << "] == " << *it << '\n';

if (auto it = ranges::adjacent_find(v, ranges::greater()); it == v.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 ["
<< ranges::distance(v.begin(), it) << "] == " << *it << '\n';
}
输出
The first adjacent pair of equal elements is at [4] == 40
The last element in the non-decreasing subsequence is at [7] == 41
本文来源于 CppReference 页面。它可能经过了修改以进行改进或适应编辑者的偏好。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。

std::ranges::adjacent_find() 算法

// (1)
constexpr I
adjacent_find( I first, S last, Pred pred = {}, Proj proj = {} );

// (2)
constexpr ranges::borrowed_iterator_t<R>
adjacent_find( R&& r, Pred pred = {}, Proj proj = {} );

参数类型是泛型的,并具有以下约束

  • I - std::forward_iterator

  • S - std::sentinel_for<I>

  • Proj - (无)

  • Pred:

    • (1) - indirect_binary_predicate<projected<I, Proj>, projected<I, Proj>>
    • (2) - indirect_binary_predicate<projected<ranges::iterator_t<R>, Proj>, projected<ranges::iterator_t<R>, Proj>>

    (为方便阅读,此处省略了 std:: 命名空间)

  • (2) - R - std::ranges::input_range

对于所有重载,Proj 模板参数的默认类型为 std::identity

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

  • (1) 元素使用 pred 进行比较(在使用投影 proj 进行投影之后)。

  • (2)(1) 相同,但使用 r 作为源范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

本页描述的函数类实体是niebloids

参数

first
last

要检查的元素范围。

r

要检查的元素范围。

pred

应用于投影元素的谓词。

proj

应用于元素的投影。

返回值

指向第一对相同元素的第一个元素的迭代器,即第一个迭代器 `it`,使得 `bool(std::invoke(pred, std::invoke(proj1, *it), std::invoke(proj, *(it + 1))))` 为 `true`。

如果未找到此类元素,则返回等于 last 的迭代器。

复杂度

谓词和投影的精确应用次数为 min((result - first) + 1, (last - first) - 1),其中 result 是返回值。

异常

(无)

可能的实现

adjacent_find(1) 和 adjacent_find(2)
struct adjacent_find_fn
{
template<std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
std::indirect_binary_predicate<
std::projected<I, Proj>,
std::projected<I, Proj>> Pred = ranges::equal_to>
constexpr I operator()(I first, S last, Pred pred = {}, Proj proj = {}) const
{
if (first == last)
return first;
auto next = ranges::next(first);
for (; next != last; ++next, ++first)
if (std::invoke(pred, std::invoke(proj, *first), std::invoke(proj, *next)))
return first;
return next;
}

template<ranges::forward_range R, class Proj = std::identity,
std::indirect_binary_predicate<
std::projected<ranges::iterator_t<R>, Proj>,
std::projected<ranges::iterator_t<R>, Proj>> Pred = ranges::equal_to>
constexpr ranges::borrowed_iterator_t<R>
operator()(R&& r, Pred pred = {}, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
}
};

inline constexpr adjacent_find_fn adjacent_find;

示例

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

int main()
{
const auto v = {0, 1, 2, 3, 40, 40, 41, 41, 5}; /*
^^ ^^ */
namespace ranges = std::ranges;

if (auto it = ranges::adjacent_find(v.begin(), v.end()); it == v.end())
std::cout << "No matching adjacent elements\n";
else
std::cout << "The first adjacent pair of equal elements is at ["
<< ranges::distance(v.begin(), it) << "] == " << *it << '\n';

if (auto it = ranges::adjacent_find(v, ranges::greater()); it == v.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 ["
<< ranges::distance(v.begin(), it) << "] == " << *it << '\n';
}
输出
The first adjacent pair of equal elements is at [4] == 40
The last element in the non-decreasing subsequence is at [7] == 41
本文来源于 CppReference 页面。它可能经过了修改以进行改进或适应编辑者的偏好。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。