std::ranges::is_partitioned
ja.cppreference.net より
| ヘッダ <algorithm>
|
||
| で定義 |
||
template< std::input_iterator I, std::sentinel_for<I> S,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
constexpr bool
is_partitioned( I first, S last, Pred pred, Proj proj = {} );
|
呼び出しシグネチャ | (1) |
template< ranges::input_range R, class Proj = std::identity,
std::indirect_unary_predicate<
std::projected<ranges::iterator_t<R>, Proj>> Pred >
constexpr bool
is_partitioned( R&& r, Pred pred, Proj proj = {} );
|
(C++20 以降) | (2) |
(C++20 以降)1)
true 範囲 [first, last) 内のすべての要素について、射影後に述語 pred を満たす要素が、満たさない要素より前に現れる場合に true を返す。また、[first, last) が空の場合にも を返す。2) (1) と同じだが、ソース範囲として
r を使用する。これは ranges::begin(r) を first として、ranges::end(r) を last として使用するのと同様である。このページで説明されている関数のようなエンティティは アルゴリズム関数オブジェクト (非形式的に niebloids として知られる) であり、以下の特徴を持つ:
- それらを呼び出す際に明示的なテンプレート実引数リストを指定することはできない。
- それらは 引数依存の名前探索 には可視ではない。
- それらのいずれかが関数呼び出し演算子の左側の名前として 通常の非修飾名前探索 によって発見された場合、引数依存の名前探索 は抑制される。
パラメータ
| first, last | - | 検査する要素の 範囲 を定義するイテレータ-センチネルペア |
| r | - | 検査する要素の範囲 |
| pred | - | 投影された要素に適用する述語 |
| proj | - | 要素に適用する投影 |
戻り値
true
範囲
[
first
,
last
)
が空であるか、または
pred
によって分割されている場合。
false
それ以外の場合。
計算量
最大で ranges:: distance ( first, last ) 回の pred および proj の適用。
実装例
struct is_partitioned_fn { template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred> constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const { for (; first != last; ++first) if (!std::invoke(pred, std::invoke(proj, *first))) break; for (; first != last; ++first) if (std::invoke(pred, std::invoke(proj, *first))) return false; return true; } template<ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>> Pred> constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const { return (*this)(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } }; inline constexpr auto is_partitioned = is_partitioned_fn(); |
例
このコードを実行
#include <algorithm> #include <array> #include <iostream> #include <numeric> #include <utility> int main() { std::array<int, 9> v; auto print = [&v](bool o) { for (int x : v) std::cout << x << ' '; std::cout << (o ? "=> " : "=> not ") << "partitioned\n"; }; auto is_even = [](int i) { return i % 2 == 0; }; std::iota(v.begin(), v.end(), 1); // or std::ranges::iota(v, 1); print(std::ranges::is_partitioned(v, is_even)); std::ranges::partition(v, is_even); print(std::ranges::is_partitioned(std::as_const(v), is_even)); std::ranges::reverse(v); print(std::ranges::is_partitioned(v.cbegin(), v.cend(), is_even)); print(std::ranges::is_partitioned(v.crbegin(), v.crend(), is_even)); }
出力:
1 2 3 4 5 6 7 8 9 => not partitioned 2 4 6 8 5 3 7 1 9 => partitioned 9 1 7 3 5 8 6 4 2 => not partitioned 9 1 7 3 5 8 6 4 2 => partitioned
関連項目
|
(C++20)
|
要素の範囲を2つのグループに分割する
(アルゴリズム関数オブジェクト) |
|
(C++20)
|
分割済み範囲の分割点を特定する
(アルゴリズム関数オブジェクト) |
|
(C++11)
|
範囲が指定された述語で分割されているかどうかを判定する
(関数テンプレート) |