std::ranges:: adjacent_find
std::ranges
| Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<algorithm>
|
||
|
调用约定
|
||
|
template
<
std::
forward_iterator
I,
std::
sentinel_for
<
I
>
S,
class
Proj
=
std::
identity
,
std::
indirect_binary_predicate
<
|
(1) | (C++20 起) |
|
template
<
ranges::
forward_range
R,
class
Proj
=
std::
identity
,
std::
indirect_binary_predicate
<
|
(2) | (C++20 起) |
在范围
[
first
,
last
)
中搜索第一对连续相等的元素。
本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:
目录 |
参数
| 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 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; |
示例
#include <algorithm> #include <functional> #include <iostream> #include <ranges> constexpr bool some_of(auto&& r, auto&& pred) // 部分但非全部 { return std::ranges::cend(r) != std::ranges::adjacent_find(r, [&pred](auto const& x, auto const& y) { return pred(x) != pred(y); }); } // 测试 some_of constexpr auto a = {0, 0, 0, 0}, b = {1, 1, 1, 0}, c = {1, 1, 1, 1}; auto is_one = [](auto x){ return x == 1; }; static_assert(!some_of(a, is_one) && some_of(b, is_one) && !some_of(c, is_one)); 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 << "未找到相邻匹配元素\n"; else std::cout << "第一对相等相邻元素位于 [" << ranges::distance(v.begin(), it) << "] == " << *it << '\n'; if (auto it = ranges::adjacent_find(v, ranges::greater()); it == v.end()) std::cout << "整个向量按升序排列\n"; else std::cout << "非递减子序列的最后一个元素位于 [" << ranges::distance(v.begin(), it) << "] == " << *it << '\n'; }
输出:
第一对相等相邻元素位于 [4] == 40 非递减子序列的最后一个元素位于 [7] == 41
参见
|
(C++20)
|
移除范围中连续的重复元素
(算法函数对象) |
|
查找首个相邻的相等元素(或满足给定谓词的元素)
(函数模板) |