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

std::replace_copy, std::replace_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 replace_copy( InputIt first, InputIt last, OutputIt d_first,

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

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

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

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

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

OutputIt replace_copy_if( InputIt first, InputIt last, OutputIt d_first,

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

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

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

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

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

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

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

目次

[編集]引数

first, last - コピーする要素の範囲
d_first - コピー先範囲の先頭
old_value - 置き換えられる要素の値
policy - 使用する実行ポリシー。 詳細は実行ポリシーを参照してください
p - 要素の値が置き換えられるべき場合に ​true を返す単項述語。

述語関数のシグネチャは以下と同等なものであるべきです。

 bool pred(const Type &a);

シグネチャが const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなりません。
TypeInputIt 型のオブジェクトの逆参照から暗黙に変換可能なものでなければなりません。 ​

new_value - 置き換える要素の値
型の要件
-
InputItInputIterator の要件を満たさなければなりません。
-
OutputItOutputIterator の要件を満たさなければなりません。
-
ForwardIt1, ForwardIt2ForwardIterator の要件を満たさなければなりません。

[編集]戻り値

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

[編集]計算量

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

[編集]例外

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

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

[編集]実装例

1つめのバージョン
template<class InputIt, class OutputIt, class T> OutputIt replace_copy(InputIt first, InputIt last, OutputIt d_first, const T& old_value, const T& new_value){for(; first != last;++first){*d_first++=(*first == old_value)? new_value :*first;}return d_first;}
2つめのバージョン
template<class InputIt, class OutputIt, class UnaryPredicate, class T> OutputIt replace_copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPredicate p, const T& new_value){for(; first != last;++first){*d_first++= p(*first )? new_value :*first;}return d_first;}

[編集]

以下のコードは、 5 より大きいすべての値をオンザフライで 99 に置き換えながら、ベクタを出力します。

#include <algorithm>#include <vector>#include <iostream>#include <iterator>#include <functional>   int main(){std::vector<int> v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; std::replace_copy_if(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "), [](int n){return n >5;}, 99);std::cout<<'\n';}

出力:

5 99 4 2 99 99 1 99 0 3

[編集]関連項目

一定の基準を満たす要素を削除します
(関数テンプレート)[edit]
close