std::remove_copy, std::remove_copy_if
| ヘッダー <algorithm> で定義 |
||
| (1) | ||
template< class InputIt, class OutputIt, class T > OutputIt remove_copy( InputIt first, InputIt last, |
(C++20 以降 constexpr) (C++26まで) |
|
| template< class InputIt, class OutputIt, class T = typename std::iterator_traits |
(C++26以降) | |
| (2) | ||
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T > |
(C++17以降) (C++26まで) |
|
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, |
(C++26以降) | |
template< class InputIt, class OutputIt, class UnaryPred > OutputIt remove_copy_if( InputIt first, InputIt last, |
(3) | (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPred > |
(4) | (C++17以降) |
範囲[first, last)から要素を別の範囲のd_firstから始まる位置にコピーしますが、特定の基準を満たす要素は省略します。
|
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以降) |
ソース範囲と宛先範囲が重なる場合、動作は未定義です。
ソース範囲と宛先範囲が重なる場合、動作は未定義です。
目次 |
[edit] パラメータ
| first, last | - | コピーする要素のソース 範囲 を定義するイテレータペア |
| d_first | - | コピー先範囲の先頭 |
| value | - | コピーしない要素の値 |
| policy | - | 使用する 実行ポリシー |
| 型要件 | ||
-InputIt は LegacyInputIterator の要件を満たす必要があります。 | ||
-OutputIt は LegacyOutputIterator の要件を満たさなければなりません。 | ||
-ForwardIt1, ForwardIt2 は LegacyForwardIterator の要件を満たさなければなりません。 | ||
-UnaryPred は Predicate の要件を満たさなければなりません。 | ||
[edit] 戻り値
最後にコピーされた要素の次を指すイテレータ。
[edit] 計算量
std::distance(first, last) を N とする
ExecutionPolicy を持つオーバーロードでは、ForwardIt1 の value_type が MoveConstructible でない場合、パフォーマンスコストが発生する可能性があります。
[edit] 例外
ExecutionPolicy というテンプレートパラメータを持つオーバーロードは、次のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外をスローし、
ExecutionPolicyが 標準ポリシー のいずれかである場合、std::terminate が呼び出されます。その他のExecutionPolicyの場合、動作は実装定義です。 - アルゴリズムがメモリの割り当てに失敗した場合、std::bad_alloc がスローされます。
[edit] 実装例
| remove_copy (1) |
|---|
template<class InputIt, class OutputIt, class T = typename std::iterator_traits<InputIt>::value_type> constexpr 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; } |
| remove_copy_if (3) |
template<class InputIt, class OutputIt, class UnaryPred> constexpr OutputIt remove_copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPred p) { for (; first != last; ++first) if (!p(*first)) *d_first++ = *first; return d_first; } |
[edit] 注意
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | アルゴリズム (1,2) のためのリスト初期化 |
[edit] 例
#include <algorithm> #include <complex> #include <iomanip> #include <iostream> #include <iterator> #include <string> #include <vector> int main() { // Erase the hash characters '#' on the fly. std::string str = "#Return #Value #Optimization"; std::cout << "before: " << std::quoted(str) << '\n'; std::cout << "after: \""; std::remove_copy(str.begin(), str.end(), std::ostream_iterator<char>(std::cout), '#'); std::cout << "\"\n"; // Erase {1, 3} value on the fly. std::vector<std::complex<double>> nums{{2, 2}, {1, 3}, {4, 8}, {1, 3}}; std::remove_copy(nums.begin(), nums.end(), std::ostream_iterator<std::complex<double>>(std::cout), #ifdef __cpp_lib_algorithm_default_value_type {1, 3}); // T gets deduced #else std::complex<double>{1, 3}); #endif }
出力
before: "#Return #Value #Optimization" after: "Return Value Optimization" (2,2)(4,8)
[edit] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 779 | C++98 | T は EqualityComparable であることが求められていましたが、ForwardIt の値型が常に T とは限らない |
必要とされる *d_first = *first 代わりに |
[edit] 関連項目
| 特定の基準を満たす要素を削除する (関数テンプレート) | |
| (C++11) |
要素の範囲を新しい場所にコピーする (関数テンプレート) |
| (C++11) |
要素を2つのグループに分割しながら範囲をコピーする (関数テンプレート) |
| (C++20)(C++20) |
特定の基準を満たす要素を除外して範囲をコピーする (アルゴリズム関数オブジェクト) |