C++ 参考手册
- C++11
- C++14
- C++17
- C++20
- C++ 编译器支持情况表
- 独立与宿主实现
- C++ 语言
- C++ 关键词
- 预处理器
- C++ 标准库头文件
- 具名要求
- 功能特性测试 (C++20)
- 工具库
- 类型支持(基本类型、RTTI、类型特性)
- 概念库 (C++20)
- 错误处理
- 动态内存管理
- 日期和时间工具
- 字符串库
- 容器库
- 迭代器库
- 范围库 (C++20)
- 算法库
- std::execution::sequenced_policy, std::execution::parallel_policy, std::execution::parallel_unsequenced_policy, std::execution::unsequenced_policy
- 有制约算法 (C++20 起)
- std::sort
- std::reduce
- std::inclusive_scan
- std::exclusive_scan
- std::random_shuffle, std::shuffle
- std::clamp
- std::equal
- std::is_permutation
- std::mismatch
- std::swap
- std::search
- std::transform
- std::for_each
- std::make_heap
- std::ranges::all_of, std::ranges::any_of, std::ranges::none_of
- std::ranges::for_each
- std::ranges::count, std::ranges::count_if
- std::ranges::mismatch, std::ranges::mismatch_result
- std::ranges::find, std::ranges::find_if, std::ranges::find_if_not
- std::ranges::adjacent_find
- std::is_execution_policy
- std::execution::seq, std::execution::par, std::execution::par_unseq, std::execution::unseq
- std::all_of, std::any_of, std::none_of
- std::for_each_n
- std::count, std::count_if
- std::adjacent_find
- std::find, std::find_if, std::find_if_not
- std::find_end
- std::find_first_of
- std::search_n
- std::lexicographical_compare
- std::lexicographical_compare_three_way
- std::copy, std::copy_if
- std::copy_n
- std::copy_backward
- std::move
- std::move_backward
- std::shift_left, std::shift_right
- std::fill
- std::fill_n
- std::generate
- std::generate_n
- std::iter_swap
- std::swap_ranges
- std::sample
- std::remove, std::remove_if
- std::replace, std::replace_if
- std::reverse
- std::rotate
- std::unique
- std::remove_copy, std::remove_copy_if
- std::replace_copy, std::replace_copy_if
- std::reverse_copy
- std::rotate_copy
- std::unique_copy
- std::is_partitioned
- std::partition_point
- std::partition
- std::partition_copy
- std::stable_partition
- std::is_sorted
- std::is_sorted_until
- std::stable_sort
- std::partial_sort
- std::partial_sort_copy
- std::nth_element
- std::lower_bound
- std::upper_bound
- std::binary_search
- std::equal_range
- std::merge
- std::inplace_merge
- std::set_difference
- std::set_intersection
- std::set_symmetric_difference
- std::set_union
- std::includes
- std::is_heap
- std::is_heap_until
- std::sort_heap
- std::push_heap
- std::pop_heap
- std::max
- std::max_element
- std::min
- std::min_element
- std::minmax
- std::minmax_element
- std::next_permutation
- std::prev_permutation
- std::iota
- std::inner_product
- std::adjacent_difference
- std::accumulate
- std::transform_reduce
- std::partial_sum
- std::transform_inclusive_scan
- std::transform_exclusive_scan
- std::qsort
- std::bsearch
- 注释
- 数值库
- 输入/输出库
- 文件系统库
- 本地化库
- 正则表达式库
- 原子操作库
- 线程支持库
- 实验性 C++ 特性
- 有用的资源
- 索引
- std 符号索引
- 协程支持 (C++20)
- C++ 关键词
位置:首页 > C++ 参考手册 >算法库 >有制约算法 (C++20 起) > std::ranges::adjacent_find
std::ranges::adjacent_find
std::indirect_binary_predicate<
std::projected<I, Proj>,
std::projected<I, Proj>> Pred = ranges::equal_to >
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>
搜索范围 [first, last)
中二个连续的相等元素。
1) 用
pred
} (在以投影 proj
投影后)比较元素。2) 同 (1) ,但以
r
为源范围,如同以 ranges::begin(r) 为 first
并以 ranges::end(r) 为 last
。此页面上描述的仿函数实体是 niebloid ,即:
实际上,它们能以函数对象,或以某些特殊编译器扩展实现。
目录 |
参数
first, last | - | 要检验的范围 |
r | - | 要检验的范围 |
pred | - | 应用到投影后元素的谓词 |
proj | - | 应用到元素的投影 |
返回值
指向首对等同元素的前一者的迭代器,即首个使得 bool(std::invoke(pred, std::invoke(proj1, *it), std::invoke(proj, *(it + 1)))) 为 true 的 it
。
若找不到这种元素,则返回等于 last
的迭代器。
复杂度
准确应用 min((result-first)+1, (last-first)-1)
次谓词与投影,其中 result
是返回值。
可能的实现
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 first; } 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; |
示例
运行此代码
#include <algorithm> #include <iostream> #include <vector> #include <functional> int main() { std::vector<int> v1{0, 1, 2, 3, 40, 40, 41, 41, 5}; namespace ranges = std::ranges; auto i1 = ranges::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 at: " << ranges::distance(v1.begin(), i1) << '\n'; } auto i2 = ranges::adjacent_find(v1, ranges::greater()); 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: " << ranges::distance(v1.begin(), i2) << '\n'; } }
输出:
The first adjacent pair of equal elements at: 4 The last element in the non-decreasing subsequence is at: 7
参阅
(C++20) |
移除范围中的连续重复元素 (niebloid) |
查找首对相邻的相同(或满足给定谓词的)元素 (函数模板) |