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 | - | 要素に適用する投影 |
戻り値
最初の同一要素ペアの最初の要素へのイテレータ、すなわち
it
が以下の条件を満たす最初のイテレータ:
bool
(
std::
invoke
(
pred,
std::
invoke
(
proj1,
*
it
)
,
std::
invoke
(
proj,
*
(
it
+
1
)
)
)
)
が
true
となる場合。
そのような要素が見つからない場合、 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)
|
範囲内の連続する重複要素を削除する
(アルゴリズム関数オブジェクト) |
|
等しい(または与えられた述語を満たす)最初の2つの隣接する要素を検索する
(関数テンプレート) |