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, |
(1) | (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T > |
(2) | (C++17以降) |
| (3) | ||
template< class InputIt, class OutputIt, class UnaryPred, class T > OutputIt replace_copy_if |
(C++20 以降 constexpr) (C++26まで) |
|
| template< class InputIt, class OutputIt, class UnaryPred, class T = typename std::iterator_traits |
(C++26以降) | |
| (4) | ||
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPred, class T > |
(C++17以降) (C++26まで) |
|
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPred, class T = typename std::iterator_traits |
(C++26以降) | |
範囲 [first, last) の要素を、d_first から始まる別の範囲にコピーし、特定の条件を満たすすべての要素を new_value で置き換えます。
|
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である。 |
(C++20まで) |
|
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> が true である。 |
(C++20以降) |
式 *first および new_value のいずれかの結果が d_first に書き込み可能でない場合、プログラムは不正です。
ソース範囲とデスティネーション範囲が重複する場合、動作は未定義です。
目次 |
[編集] パラメーター
| first, last | - | コピーする要素のソース 範囲 を定義するイテレータペア |
| d_first | - | コピー先範囲の先頭 |
| old_value | - | 置き換える要素の値 |
| policy | - | 使用する 実行ポリシー |
| p | - | 要素値を置換する必要がある場合に true を返す単項述語。 式 p(v) は、型 (const の可能性あり) |
| new_value | - | 置き換えに使用する値 |
| 型要件 | ||
-InputIt は LegacyInputIterator の要件を満たす必要があります。 | ||
-OutputIt は LegacyOutputIterator の要件を満たさなければなりません。 | ||
-ForwardIt1, ForwardIt2 は LegacyForwardIterator の要件を満たさなければなりません。 | ||
[編集] 戻り値
最後にコピーされた要素の次を指すイテレータ。
[編集] 計算量
std::distance(first, last) を N とする
[編集] 例外
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}); // Possible, since the T is deduced. #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) |
特定の基準を満たす要素を別の値に置き換えながら範囲をコピーする (アルゴリズム関数オブジェクト) |