GeezerVison is showing cpprefjp.github.io — std::ranges::remove_copy_if - cpprefjp C++日本語リファレンス
最終更新日時:
2026年09月11日 13時06分31秒 (JST)
Akira Takahashi
が更新
履歴
編集
function template
<algorithm>
std::ranges:: remove_copy_if (C++20)
namespace std :: ranges {
template < input_iterator I ,
sentinel_for < I > S ,
weakly_incrementable O ,
class Proj = identity ,
indirect_unary_predicate < projected < I , Proj >> Pred >
requires indirectly_copyable < I , O >
constexpr remove_copy_if_result < I , O >
remove_copy_if ( I first ,
S last ,
O result ,
Pred pred ,
Proj proj = {}); // (1) C++20
template < input_range R ,
weakly_incrementable O ,
class Proj = identity ,
indirect_unary_predicate < projected < iterator_t < R > , Proj >> Pred >
requires indirectly_copyable < iterator_t < R > , O >
constexpr remove_copy_if_result < borrowed_iterator_t < R > , O >
remove_copy_if ( R && r ,
O result ,
Pred pred ,
Proj proj = {}); // (2) C++20
template < execution-policy Ep ,
random_access_iterator I ,
sized_sentinel_for < I > S ,
random_access_iterator O ,
sized_sentinel_for < O > OutS ,
class Proj = identity ,
indirect_unary_predicate < projected < I , Proj >> Pred >
requires indirectly_copyable < I , O >
remove_copy_if_result < I , O >
remove_copy_if ( Ep && exec ,
I first ,
S last ,
O result ,
OutS result_last ,
Pred pred ,
Proj proj = {}); // (3) C++26
template < execution-policy Ep ,
sized-random-access-range R ,
sized-random-access-range OutR ,
class Proj = identity ,
indirect_unary_predicate < projected < iterator_t < R > , Proj >> Pred >
requires indirectly_copyable < iterator_t < R > , iterator_t < OutR >>
remove_copy_if_result < borrowed_iterator_t < R > , borrowed_iterator_t < OutR >>
remove_copy_if ( Ep && exec ,
R && r ,
OutR && result_r ,
Pred pred ,
Proj proj = {}); // (4) C++26
}
概要
条件を満たす要素を除け、その結果を出力の範囲へコピーする。
(1): イテレータ範囲を指定する
(2): Rangeを直接指定する
(3): (1)の並列アルゴリズム版。実行ポリシーを指定する
(4): (2)の並列アルゴリズム版。実行ポリシーを指定する
事前条件
[first,last) と [result,result + (last - first)) は重なってはならない。
効果
[first,last) 内にあるイテレータ i について、pred(*i) != false でない要素を result へコピーする
戻り値
{ .in = last, .out = result + N }
ここでNは、predを満たさないためコピーされた要素の個数である。
計算量
正確に last - first 回の述語の適用を行う
備考
安定
例
基本的な使い方
出力
2,2,
並列アルゴリズムの例 (C++26)
#include <algorithm>
#include <iostream>
#include <vector>
#include <execution>
int main () {
std::vector < int > src = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
std::vector < int > dst ( src . size ());
// 並列に奇数を除去してコピーする
auto result = std::ranges::remove_copy_if ( std::execution::par , src , dst ,
[]( int x ) { return x % 2 != 0 ; });
for ( auto it = dst . begin (); it != result . out ; ++ it ) {
std::cout << * it << ' ' ;
}
std::cout << std::endl ;
}
出力
2 4 6 8 10
バージョン
言語
処理系
参照