◐ Shell
clean mode source ↗

std::remove_copy, std::remove_copy_if - cppreference.com

提供: cppreference.com

<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>

ヘッダ <algorithm> で定義

(1)

template< class InputIt, class OutputIt, class T > OutputIt remove_copy( InputIt first, InputIt last, OutputIt d_first, const T& value );

(C++20未満)

template< class InputIt, class OutputIt, class T > constexpr OutputIt remove_copy( InputIt first, InputIt last, OutputIt d_first, const T& value );

(C++20以上)

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T > ForwardIt2 remove_copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first, const T& value );

(2) (C++17以上)
(3)

template< class InputIt, class OutputIt, class UnaryPredicate > OutputIt remove_copy_if( InputIt first, InputIt last, OutputIt d_first, UnaryPredicate p );

(C++20未満)

template< class InputIt, class OutputIt, class UnaryPredicate > constexpr OutputIt remove_copy_if( InputIt first, InputIt last, OutputIt d_first, UnaryPredicate p );

(C++20以上)

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPredicate > ForwardIt2 remove_copy_if( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first, UnaryPredicate p );

(4) (C++17以上)

範囲 [first, last) の要素を、特定の基準を満たす要素を除き、 d_first から始まる別の範囲にコピーします。 コピー元とコピー先の範囲はオーバーラップしていてはなりません。

1) value と等しいすべての要素を無視します。

3) 述語 ptrue を返すすべての要素を無視します。

2,4) (1,3) と同じですが、 policy に従って実行されます。 このオーバーロードは、std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します。

引数

first, last - コピーする要素の範囲
d_first - コピー先範囲の先頭
value - コピーしない要素の値
policy - 使用する実行ポリシー。 詳細は実行ポリシーを参照してください
型の要件
-InputItLegacyInputIterator の要件を満たさなければなりません。
-OutputItLegacyOutputIterator の要件を満たさなければなりません。
-ForwardIt1, ForwardIt2LegacyForwardIterator の要件を満たさなければなりません。
-UnaryPredicatePredicate の要件を満たさなければなりません。

戻り値

最後にコピーされた要素の次の要素を指すイテレータ。

計算量

ちょうど last - first 回の述語の適用。

ExecutionPolicy を取るオーバーロードの場合、 ForwardIt1 の value_type が MoveConstructible でなければ、性能上のコストが生じることがあります。

例外

テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。

  • アルゴリズムの一部として呼び出された関数の実行が例外を投げ、 ExecutionPolicy標準のポリシーのいずれかの場合は、 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

関連項目