std::replace_copy, std::replace_copy_if
ヘッダ <algorithm> で定義
|
||
(1) | ||
template< class InputIt, class OutputIt, class T > OutputIt replace_copy( InputIt first, InputIt last, OutputIt d_first, |
(C++20未満) | |
template< class InputIt, class OutputIt, class T > constexpr OutputIt replace_copy( InputIt first, InputIt last, OutputIt d_first, |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T > ForwardIt2 replace_copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, |
(2) | (C++17以上) |
(3) | ||
template< class InputIt, class OutputIt, class UnaryPredicate, class T > OutputIt replace_copy_if( InputIt first, InputIt last, OutputIt d_first, |
(C++20未満) | |
template< class InputIt, class OutputIt, class UnaryPredicate, class T > constexpr OutputIt replace_copy_if( InputIt first, InputIt last, OutputIt d_first, |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPredicate, class T > ForwardIt2 replace_copy_if( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, |
(4) | (C++17以上) |
範囲 [first, last)
の要素を、特定の基準を満たすすべての要素を new_value
に置き換えながら、 d_first
から始まる別の範囲にコピーします。 コピー元とコピー先の範囲はオーバーラップしていてはなりません。
old_value
と等しいすべての要素を置き換えます。p
が true を返すすべての要素を置き換えます。policy
に従って実行されます。 このオーバーロードは、std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true場合にのみ、オーバーロード解決に参加します目次 |
[編集] 引数
first, last | - | コピーする要素の範囲 |
d_first | - | コピー先範囲の先頭 |
old_value | - | 置き換えられる要素の値 |
policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
p | - | 要素の値が置き換えられるべき場合に true を返す単項述語。 式 p(v) は |
new_value | - | 置き換える要素の値 |
型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
| ||
-OutputIt は LegacyOutputIterator の要件を満たさなければなりません。
| ||
-ForwardIt1, ForwardIt2 は LegacyForwardIterator の要件を満たさなければなりません。
|
[編集] 戻り値
最後にコピーされた要素の次の要素を指すイテレータ。
[編集] 計算量
ちょうど last - first
個の述語の適用。
[編集] 例外
テンプレート引数 ExecutionPolicy
を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicy
が標準のポリシーのいずれかの場合は、 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
[編集] 関連項目
一定の基準を満たす要素を削除します (関数テンプレート) |