The Wayback Machine - https://web.archive.org/web/20180601205520/http://ja.cppreference.com:80/w/cpp/algorithm/remove_copy
名前空間
変種
操作

std::remove_copy, std::remove_copy_if

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
バイナリサーチ操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)
順列
数値演算
C のライブラリ
 
ヘッダ <algorithm> で定義
(1)
template<class InputIt, class OutputIt, class T >

OutputIt remove_copy( InputIt first, InputIt last, OutputIt d_first,

                      const T& value );
(C++20以前)
template<class InputIt, class OutputIt, class T >

constexpr OutputIt remove_copy( InputIt first, InputIt last, OutputIt d_first,

                                const T& value );
(C++20およびそれ以降)
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T >

ForwardIt2 remove_copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,

                        ForwardIt2 d_first, const T& value );
(2) (C++17およびそれ以降)
(3)
template<class InputIt, class OutputIt, class UnaryPredicate >

OutputIt remove_copy_if( InputIt first, InputIt last, OutputIt d_first,

                         UnaryPredicate p );
(C++20以前)
template<class InputIt, class OutputIt, class UnaryPredicate >

constexpr OutputIt remove_copy_if( InputIt first, InputIt last, OutputIt d_first,

                                   UnaryPredicate p );
(C++20およびそれ以降)
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPredicate >

ForwardIt2 remove_copy_if( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,

                           ForwardIt2 d_first, UnaryPredicate p );
(4) (C++17およびそれ以降)

範囲 [first, last) の要素を、特定の基準を満たす要素を除き、 d_first から始まる別の範囲にコピーします。 コピー元とコピー先の範囲はオーバーラップしていてはなりません。

1)value と等しいすべての要素を無視します。
3) 述語 ptrue を返すすべての要素を無視します。
2,4)(1,3) と同じですが、 policy に従って実行されます。 このオーバーロードは、std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します。

目次

[編集]引数

first, last - コピーする要素の範囲
d_first - コピー先範囲の先頭
value - コピーしない要素の値
policy - 使用する実行ポリシー。 詳細は実行ポリシーを参照してください
型の要件
-
InputItInputIterator の要件を満たさなければなりません。
-
OutputItOutputIterator の要件を満たさなければなりません。
-
ForwardIt1, ForwardIt2ForwardIterator の要件を満たさなければなりません。
-
UnaryPredicatePredicate の要件を満たさなければなりません。

[編集]戻り値

最後にコピーされた要素の次の要素を指すイテレータ。

[編集]計算量

ちょうど last - first 回の述語の適用。

ExecutionPolicy を取るオーバーロードの場合、 ForwardIt1 の value_type が MoveConstructible でなければ、性能上のコストが生じることがあります。

[編集]例外

テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。

  • アルゴリズムの一部として呼び出された関数の実行が例外を投げ、 ExecutionPolicy が3つの標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆる ExecutionPolicy については、動作は処理系定義です。
  • アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。

[編集]実装例

1つめのバージョン
template<class InputIt, class OutputIt, class T> OutputIt remove_copy(InputIt first, InputIt last, OutputIt d_first, const T& value){for(; first != last;++first){if(!(*first == value)){*d_first++=*first;}}return d_first;}
2つめのバージョン
template<class InputIt, class OutputIt, class UnaryPredicate> OutputIt remove_copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPredicate p){for(; first != last;++first){if(!p(*first)){*d_first++=*first;}}return d_first;}

[編集]

以下のコードはオンザフライで空白を消去しながら文字列を出力します。

#include <algorithm>#include <iterator>#include <string>#include <iostream>int main(){std::string str ="Text with some spaces";std::cout<<"before: "<< str <<"\n";   std::cout<<"after: "; std::remove_copy(str.begin(), str.end(), std::ostream_iterator<char>(std::cout), ' ');std::cout<<'\n';}

出力:

before: Text with some spaces after: Textwithsomespaces

[編集]関連項目

一定の基準を満たす要素を削除します
(関数テンプレート)[edit]
指定範囲の要素を新しい位置にコピーします
(関数テンプレート)[edit]
指定範囲の要素を2つのグループに分割しながらコピーします
(関数テンプレート)[edit]
close