GeezerVison is showing cpprefjp.github.io — std::ranges::partition - cpprefjp C++日本語リファレンス
最終更新日時:
2026年09月11日 13時06分31秒 (JST)
Akira Takahashi
が更新
履歴
編集
function template
<algorithm>
std::ranges:: partition (C++20)
namespace std :: ranges {
template < permutable I ,
sentinel_for < I > S ,
class Proj = identity ,
indirect_unary_predicate < projected < I , Proj >> Pred >
constexpr subrange < I >
partition ( I first ,
S last ,
Pred pred ,
Proj proj = {}); // (1) C++20
template < forward_range R ,
class Proj = identity ,
indirect_unary_predicate < projected < iterator_t < R > , Proj >> Pred >
requires permutable < iterator_t < R >>
constexpr borrowed_subrange_t < R >
partition ( R && r ,
Pred pred ,
Proj proj = {}); // (2) C++20
template < execution-policy Ep ,
random_access_iterator I ,
sized_sentinel_for < I > S ,
class Proj = identity ,
indirect_unary_predicate < projected < I , Proj >> Pred >
requires permutable < I >
subrange < I >
partition ( Ep && exec ,
I first ,
S last ,
Pred pred ,
Proj proj = {}); // (3) C++26
template < execution-policy Ep ,
sized-random-access-range R ,
class Proj = identity ,
indirect_unary_predicate < projected < iterator_t < R > , Proj >> Pred >
requires permutable < iterator_t < R >>
borrowed_subrange_t < R >
partition ( Ep && exec ,
R && r ,
Pred pred ,
Proj proj = {}); // (4) C++26
}
概要
与えられた範囲を条件によって区分化 する。
(1): イテレータ範囲を指定する
(2): Rangeを直接指定する
(3): (1)の並列アルゴリズム版。実行ポリシーを指定する
(4): (2)の並列アルゴリズム版。実行ポリシーを指定する
効果
[first,last) 内にある pred を満たす全ての要素を、pred を満たさない全ての要素より前に移動させる。
戻り値
{i, last}
ただし、[first,i) 内にあるイテレータ j について pred(*j) != false を満たし、[i,last) 内にあるイテレータ k について pred(*k) == false を満たすようなイテレータ (つまり、区分化 された境界を指すイテレータ) を i とする。
計算量
N = last - firstとして
(1), (2) : I が bidirectional_iterator のモデルとなる場合、最大で N / 2 回 swap され、そうでない場合、最大で N 回 swap される。それに加えて、正確に N 回だけ述語が適用される
例
基本的な使い方
出力
4
2
----
3
1
5
並列アルゴリズムの例 (C++26)
出力例
even: 8 2 6 4
odd: 5 3 7 1
バージョン
言語
処理系
参照