std::ranges::is_partitioned
From cppreference.com
| ヘッダー <algorithm> で定義 |
||
| 呼び出しシグネチャ |
||
| template< std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, |
(1) | (C++20以降) |
| template< ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate< |
(2) | (C++20以降) |
1) 述語 pred を射影した結果が真となる要素が、偽となる要素すべてよりも前に現れる場合、範囲
[first, last) 内のすべての要素について true を返します。また、[first, last) が空の場合も true を返します。このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、
- これらのいずれかを呼び出す際に、明示的なテンプレート引数リストを指定することはできません。
- これらのいずれも実引数依存の名前探索には見えません。
- これらのいずれかが関数呼び出し演算子の左側の名前として通常の非修飾名探索によって見つかった場合、実引数依存の名前探索は抑制されます。
目次 |
[編集] Parameters
| first, last | - | 調査する要素の範囲を定義するイテレータとセンチネルのペア |
| r | - | 調査する要素の範囲 |
| pred | - | 射影された要素に適用する述語 |
| proj | - | 要素に適用する射影 |
[編集] Return value
範囲 [first, last) が空であるか、pred によって分割されている場合は true を、それ以外の場合は false を返します。
[編集] Complexity
ranges::distance(first, last) 回以下の pred および proj の適用。
[編集] Possible implementation
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(); |
[編集] Example
このコードを実行
#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
[編集] See also
| (C++20) |
要素の範囲を2つのグループに分割する (アルゴリズム関数オブジェクト) |
| (C++20) |
パーティション化された範囲のパーティションポイントを見つける (アルゴリズム関数オブジェクト) |
| (C++11) |
範囲が指定された述語によってパーティション化されているかどうかを判断する (関数テンプレート) |