GeezerVison is showing cpprefjp.github.io — std::partition_copy - cpprefjp C++日本語リファレンス
最終更新日時:
2026年09月19日 21時31分51秒 (JST)
rotarymars
が更新
履歴
編集
function template
<algorithm>
std:: partition_copy (C++11)
namespace std {
template < class InputIterator ,
class OutputIterator1 ,
class OutputIterator2 ,
class Predicate >
pair < OutputIterator1 , OutputIterator2 >
partition_copy ( InputIterator first ,
InputIterator last ,
OutputIterator1 out_true ,
OutputIterator2 out_false ,
Predicate pred ); // (1) C++11
template < class InputIterator ,
class OutputIterator1 ,
class OutputIterator2 ,
class Predicate >
constexpr pair < OutputIterator1 , OutputIterator2 >
partition_copy ( InputIterator first ,
InputIterator last ,
OutputIterator1 out_true ,
OutputIterator2 out_false ,
Predicate pred ); // (1) C++20
template < class ExecutionPolicy ,
class ForwardIterator ,
class ForwardIterator1 ,
class ForwardIterator2 ,
class Predicate >
pair < ForwardIterator1 , ForwardIterator2 >
partition_copy ( ExecutionPolicy && exec ,
ForwardIterator first ,
ForwardIterator last ,
ForwardIterator1 out_true ,
ForwardIterator2 out_false ,
Predicate pred ); // (2) C++17
}
概要
イテレータ範囲[first, last)を条件を満たすか満たさないかで、2つの出力範囲へ分けてコピーする。
この関数は、入力のイテレータ範囲のうち、条件を満たす要素を出力イテレータ範囲out_true、条件を満たさない要素を出力イテレータ範囲out_falseにコピーする。
テンプレートパラメータ制約
InputIterator の value type は CopyAssignable で、out_true と out_false の OutputIterator へ書き込み可能で、Predicate の引数型へ変換可能であること
事前条件
入力範囲は出力範囲のどちらとも重なっていてはならない
効果
イテレータ範囲[first,last) 内にあるそれぞれのイテレータ i について、pred(*i) が true なら *i を out_true へコピーし、そうでない場合は out_false へコピーする。
戻り値
first には out_true の終端が、second には out_false の終端が格納されたpair オブジェクトを返す。
計算量
正確に 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 << " : " ;
std::for_each ( v.begin() , v.end() , []( int x ) {
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::partition_copy ( v.begin() , v.end() ,
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,
バージョン
言語
処理系
関連項目
参照