std::replace_copy, std::replace_copy_if
| ヘッダ <algorithm>
|
||
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 );
|
で定義 (1) | (C++20 から constexpr) |
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 以上) |
template< class InputIt, class OutputIt, class UnaryPred, class T >
OutputIt replace_copy_if
( InputIt first, InputIt last, OutputIt d_first,
UnaryPred p, const T& new_value );
|
(3) | (C++26 まで) (C++20 から constexpr) |
template< class InputIt, class OutputIt, class UnaryPred,
class T = typename std::iterator_traits
<OutputIt>::value_type >
constexpr OutputIt replace_copy_if
( InputIt first, InputIt last, OutputIt d_first,
UnaryPred p, const T& new_value );
|
(C++26 以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class UnaryPred, class T >
ForwardIt2 replace_copy_if
( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,
UnaryPred p, const T& new_value );
|
(4) | (C++17 以上) (C++26 まで) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class UnaryPred, class T = typename std::iterator_traits
<ForwardIt2>::value_type >
ForwardIt2 replace_copy_if
( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,
UnaryPred p, const T& new_value );
|
(C++26 以上) | |
範囲 [first, last) の要素を d_first で始まる別の範囲にコピーしつつ、特定の条件を満たすすべての要素を new_value に置き換えます。
old_value と等しいすべての要素を置き換えます (operator== を使用)。p が true を返すすべての要素を置き換えます。policy これらのオーバーロードは、以下の式の値が true|
|
(C++20 まで) |
|
|
(C++20 以上) |
式 *first と new_value の結果のいずれかが writable ではない場合、プログラムは ill-formed です。
d_firstコピー元とコピー先の範囲が重なっている場合、動作は未定義です。
パラメータ
| first, last | - | コピーする要素のソース 範囲 を定義するイテレータのペア |
| d_first | - | コピー先範囲の先頭 |
| old_value | - | 置換する要素の値 |
| policy | - | 使用する 実行ポリシー |
| p | - |
要素値を置換すべき場合に
true
を返す単項述語。
式
p
(
v
)
は、
|
| new_value | - | 置換に使用する値 |
| 型要件 | ||
-
InputIt
は
LegacyInputIterator
の要件を満たさなければならない。
|
||
-
OutputIt
は
LegacyOutputIterator
の要件を満たさなければならない。
|
||
-
ForwardIt1, ForwardIt2
は
LegacyForwardIterator
の要件を満たさなければならない。
|
||
戻り値
コピーされた最後の要素の次の要素を指すイテレータ。
計算量
与えられた N を std:: distance ( first, last ) として:
例外
ExecutionPolicyという名前のテンプレートパラメータを持つオーバーロードは、
以下のようにエラーを報告します:
-
アルゴリズムの一部として呼び出された関数の実行が例外をスローした場合、
ExecutionPolicyが 標準ポリシー のいずれかであるとき、 std::terminate が呼び出されます。それ以外のExecutionPolicyについては、動作は実装定義です。 - アルゴリズムがメモリの確保に失敗した場合、 std::bad_alloc がスローされます。
実装例
| replace_copy (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; } |
| replace_copy_if (3) |
template<class InputIt, class OutputIt, class UnaryPred, class T = typename std::iterator_traits<ForwardIt>::value_type> OutputIt replace_copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPred p, const T& new_value) { for (; first != last; ++first) *d_first++ = p(*first) ? new_value : *first; return d_first; } |
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type
|
202403
|
(C++26) | リスト初期化 アルゴリズム用 ( 3,4 ) |
例
#include <algorithm> #include <complex> #include <iostream> #include <vector> void println(const auto& seq) { for (const auto& e : seq) std::cout << e << ' '; std::cout << '\n'; } int main() { std::vector<short> src{3, 1, 4, 1, 5, 9, 2, 6, 5}; println(src); std::vector<int> dst(src.size()); std::replace_copy_if(src.cbegin(), src.cend(), dst.begin(), [](short n){ return n > 5; }, 0); println(dst); std::vector<std::complex<double>> src2{{1, 3}, {2, 4}, {3, 5}}, dst2(src2.size()); println(src2); #ifdef __cpp_lib_algorithm_default_value_type std::replace_copy_if(src2.cbegin(), src2.cend(), dst2.begin(), [](std::complex<double> z){ return std::abs(z) < 5; }, {4, 2}); // 可能、Tが推論されるため #else std::replace_copy_if(src2.cbegin(), src2.cend(), dst2.begin(), [](std::complex<double> z){ return std::abs(z) < 5; }, std::complex<double>{4, 2}); #endif println(dst2); }
出力:
3 1 4 1 5 9 2 6 5 3 1 4 1 5 0 2 0 5 (1,3) (2,4) (3,5) (4,2) (4,2) (3,5)
不具合報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 283 | C++98 |
T
は
CopyAssignable
であることが要求されていた
( EqualityComparable は
replace_copy
の場合)
しかし
InputIt
の値型が常に
T
とは限らない
|
要求を削除 |
| LWG 337 | C++98 |
replace_copy_if
は
InputIt
が
LegacyIterator [1] の要件を満たすことのみを要求 |
LegacyInputIterator に修正 |
-
↑
C++標準における実際の欠陥は、テンプレートパラメータ
InputIteratorがIteratorとして誤って指定されていたことです。これは型要件に影響を与えます。なぜならC++標準では、アルゴリズムライブラリの関数テンプレートにおいて、名前がIteratorで終わるテンプレート型パラメータは、対応するイテレータカテゴリの型要件を暗示するためです。
関連項目
| 特定の条件を満たすすべての値を別の値で置き換える (関数テンプレート & アルゴリズム関数オブジェクト) | |
(C++20)(C++20) |
|
| 特定の条件を満たす要素を削除する (関数テンプレート & アルゴリズム関数オブジェクト) | |
(C++20)(C++20) |
|
(C++20)(C++20) |
範囲をコピーし、特定の条件を満たす要素を別の値で置き換える (アルゴリズム関数オブジェクト) |