std::remove_copy, std::remove_copy_if
ヘッダ <algorithm> で定義 | ||
(1) | ||
template<class InputIt, class OutputIt, class T > OutputIt remove_copy( InputIt first, InputIt last, OutputIt d_first, | (C++20以前) | |
template<class InputIt, class OutputIt, class T > constexpr OutputIt remove_copy( InputIt first, InputIt last, OutputIt d_first, | (C++20およびそれ以降) | |
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T > ForwardIt2 remove_copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, | (2) | (C++17およびそれ以降) |
(3) | ||
template<class InputIt, class OutputIt, class UnaryPredicate > OutputIt remove_copy_if( InputIt first, InputIt last, OutputIt d_first, | (C++20以前) | |
template<class InputIt, class OutputIt, class UnaryPredicate > constexpr OutputIt remove_copy_if( InputIt first, InputIt last, OutputIt d_first, | (C++20およびそれ以降) | |
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPredicate > ForwardIt2 remove_copy_if( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, | (4) | (C++17およびそれ以降) |
範囲 [first, last)
の要素を、特定の基準を満たす要素を除き、 d_first
から始まる別の範囲にコピーします。 コピー元とコピー先の範囲はオーバーラップしていてはなりません。
value
と等しいすべての要素を無視します。p
が true を返すすべての要素を無視します。policy
に従って実行されます。 このオーバーロードは、std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します。目次 |
[編集]引数
first, last | - | コピーする要素の範囲 |
d_first | - | コピー先範囲の先頭 |
value | - | コピーしない要素の値 |
policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
型の要件 | ||
-InputIt は InputIterator の要件を満たさなければなりません。 | ||
-OutputIt は OutputIterator の要件を満たさなければなりません。 | ||
-ForwardIt1, ForwardIt2 は ForwardIterator の要件を満たさなければなりません。 | ||
-UnaryPredicate は Predicate の要件を満たさなければなりません。 |
[編集]戻り値
最後にコピーされた要素の次の要素を指すイテレータ。
[編集]計算量
ちょうど last - first
回の述語の適用。
ExecutionPolicy を取るオーバーロードの場合、 ForwardIt1
の value_type が MoveConstructible
でなければ、性能上のコストが生じることがあります。
[編集]例外
テンプレート引数 ExecutionPolicy
を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicy
が3つの標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicy
については、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
[編集]実装例
1つめのバージョン |
---|
template<class InputIt, class OutputIt, class T> 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;} |
2つめのバージョン |
template<class InputIt, class OutputIt, class UnaryPredicate> OutputIt remove_copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPredicate p){for(; first != last;++first){if(!p(*first)){*d_first++=*first;}}return d_first;} |
[編集]例
以下のコードはオンザフライで空白を消去しながら文字列を出力します。
#include <algorithm>#include <iterator>#include <string>#include <iostream>int main(){std::string str ="Text with some spaces";std::cout<<"before: "<< str <<"\n"; std::cout<<"after: "; std::remove_copy(str.begin(), str.end(), std::ostream_iterator<char>(std::cout), ' ');std::cout<<'\n';}
出力:
before: Text with some spaces after: Textwithsomespaces
[編集]関連項目
一定の基準を満たす要素を削除します (関数テンプレート) | |
(C++11) | 指定範囲の要素を新しい位置にコピーします (関数テンプレート) |
(C++11) | 指定範囲の要素を2つのグループに分割しながらコピーします (関数テンプレート) |