GeezerVison is showing cpprefjp.github.io — std::ranges::partition_copy - cpprefjp C++日本語リファレンス
最終更新日時:
2026年09月11日 13時06分31秒 (JST)
Akira Takahashi
が更新
履歴
編集
function template
<algorithm>
std::ranges:: partition_copy (C++20)
namespace std :: ranges {
template < input_iterator I ,
sentinel_for < I > S ,
weakly_incrementable O1 ,
weakly_incrementable O2 ,
class Proj = identity ,
indirect_unary_predicate < projected < I , Proj >> Pred >
requires indirectly_copyable < I , O1 > && indirectly_copyable < I , O2 >
constexpr partition_copy_result < I , O1 , O2 >
partition_copy ( I first ,
S last , O1 out_true ,
O2 out_false ,
Pred pred ,
Proj proj = {}); // (1) C++20
template < input_range R ,
weakly_incrementable O1 ,
weakly_incrementable O2 ,
class Proj = identity ,
indirect_unary_predicate < projected < iterator_t < R > , Proj >> Pred >
requires indirectly_copyable < iterator_t < R > , O1 > &&
indirectly_copyable < iterator_t < R > , O2 >
constexpr partition_copy_result < borrowed_iterator_t < R > , O1 , O2 >
partition_copy ( R && r ,
O1 out_true ,
O2 out_false ,
Pred pred ,
Proj proj = {}); // (2) C++20
template < execution-policy Ep ,
random_access_iterator I ,
sized_sentinel_for < I > S ,
random_access_iterator O1 ,
sized_sentinel_for < O1 > OutS1 ,
random_access_iterator O2 ,
sized_sentinel_for < O2 > OutS2 ,
class Proj = identity ,
indirect_unary_predicate < projected < I , Proj >> Pred >
requires indirectly_copyable < I , O1 > && indirectly_copyable < I , O2 >
partition_copy_result < I , O1 , O2 >
partition_copy ( Ep && exec ,
I first ,
S last ,
O1 out_true ,
OutS1 out_true_last ,
O2 out_false ,
OutS2 out_false_last ,
Pred pred ,
Proj proj = {}); // (3) C++26
template < execution-policy Ep ,
sized-random-access-range R ,
sized-random-access-range OutR1 ,
sized-random-access-range OutR2 ,
class Proj = identity ,
indirect_unary_predicate < projected < iterator_t < R > , Proj >> Pred >
requires indirectly_copyable < iterator_t < R > , iterator_t < OutR1 >> &&
indirectly_copyable < iterator_t < R > , iterator_t < OutR2 >>
partition_copy_result < borrowed_iterator_t < R > , borrowed_iterator_t < OutR1 > , borrowed_iterator_t < OutR2 >>
partition_copy ( Ep && exec ,
R && r ,
OutR1 && out_true ,
OutR2 && out_false ,
Pred pred ,
Proj proj = {}); // (4) C++26
}
概要
与えられた範囲を条件によって 2 つの出力の範囲へ分けてコピーする。
(1): イテレータ範囲を指定する
(2): Rangeを直接指定する
(3): (1)の並列アルゴリズム版。実行ポリシーを指定する
(4): (2)の並列アルゴリズム版。実行ポリシーを指定する
事前条件
入力範囲は出力範囲のどちらとも重なっていてはならない。
効果
[first,last) 内にあるそれぞれのイテレータ i について、pred(*i) が true なら *i を out_true へコピーし、そうでない場合は out_false へコピーする。
戻り値
o1 を out_true の終端、o2 を out_false の終端とするとき、{ .in = last, .out1 = o1, .out2 = o2 }
計算量
正確に last - first 回述語が適用される。
例
基本的な使い方
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
void print ( const std::string & name , const std::vector < int >& v )
{
std::cout << name << " : " ;
for ( int x : v ) {
std::cout << x << "," ;
}
std::cout << std::endl ;
}
bool is_even ( int x ) { return x % 2 == 0 ; }
int main ()
{
std::vector < int > v = { 1 , 2 , 3 , 4 , 5 };
// 偶数グループと奇数グループに分ける
std::vector < int > evens ;
std::vector < int > odds ;
std::ranges::partition_copy ( v , std::back_inserter ( evens ), std::back_inserter ( odds ), is_even );
:: print ( "v" , v );
:: print ( "evens" , evens );
:: print ( "odds" , odds );
}
出力
v : 1,2,3,4,5,
evens : 2,4,
odds : 1,3,5,
並列アルゴリズムの例 (C++26)
#include <algorithm>
#include <execution>
#include <iostream>
#include <vector>
int main () {
std::vector < int > v = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
std::vector < int > evens ( 8 );
std::vector < int > odds ( 8 );
// 並列に偶数と奇数に分類してコピー
auto [ in_end , even_end , odd_end ] =
std::ranges::partition_copy ( std::execution::par , v ,
evens , odds ,
[]( int x ) { return x % 2 == 0 ; });
std::cout << "evens: " ;
for ( auto it = evens . begin (); it != even_end ; ++ it ) {
std::cout << * it << ' ' ;
}
std::cout << std::endl ;
std::cout << "odds: " ;
for ( auto it = odds . begin (); it != odd_end ; ++ it ) {
std::cout << * it << ' ' ;
}
std::cout << std::endl ;
}
出力
evens: 2 4 6 8
odds: 1 3 5 7
バージョン
言語
処理系
参照