名前空間
変種
操作

std::replace_copy, std::replace_copy_if

From cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとRangeアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
実行ポリシー (C++17)
シーケンスを変更しない操作
一括操作
(C++17)
検索操作
(C++11)                (C++11)(C++11)

シーケンスを変更する操作
コピー操作
(C++11)
(C++11)
スワップ操作
変換操作
replace_copyreplace_copy_if

生成操作
削除操作
順序変更操作
(C++17まで)(C++11)
(C++20)(C++20)
サンプリング操作
(C++17)

ソートおよび関連操作
パーティション操作
ソート操作
二分探索操作
(パーティション化された範囲)
集合操作 (ソート済み範囲)
マージ操作 (ソート済み範囲)
ヒープ操作
最小/最大操作
(C++11)
(C++17)
辞書順比較操作
順列操作
Cライブラリ
数値演算
未初期化メモリに対する操作
 
ヘッダー <algorithm> で定義
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 );
(1) (C++20 以降 constexpr)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2, class T >
ForwardIt2 replace_copy
    ( ExecutionPolicy&& policy,
      ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,

      const T& old_value, const T& new_value );
(2) (C++17以降)
(3)
template< class InputIt, class OutputIt, class UnaryPred, class T >

OutputIt replace_copy_if
    ( InputIt first, InputIt last, OutputIt d_first,

      UnaryPred p, const T& new_value );
(C++20 以降 constexpr)
(C++26まで)
template< class InputIt, class OutputIt, class UnaryPred,

          class T = typename std::iterator_traits
                        <OutputIt>::value_type >
constexpr OutputIt replace_copy_if
    ( InputIt first, InputIt last, OutputIt d_first,

      UnaryPred p, const T& new_value );
(C++26以降)
(4)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,

          class UnaryPred, class T >
ForwardIt2 replace_copy_if
    ( ExecutionPolicy&& policy,
      ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,

      UnaryPred p, const T& new_value );
(C++17以降)
(C++26まで)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,

          class UnaryPred, class T = typename std::iterator_traits
                                         <ForwardIt2>::value_type >
ForwardIt2 replace_copy_if
    ( ExecutionPolicy&& policy,
      ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,

      UnaryPred p, const T& new_value );
(C++26以降)

範囲 [firstlast) の要素を、d_first から始まる別の範囲にコピーし、特定の条件を満たすすべての要素を new_value で置き換えます。

1) old_value と等しいすべての要素を ( operator== を使用して) 置き換えます。
3) 述語 ptrue を返すすべての要素を置き換えます。
2,4) (1,3)と同じだが、policyに従って実行される。
これらのオーバーロードは、以下のすべての条件が満たされた場合にのみオーバーロード解決に参加する。

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 の可能性あり) VT のすべての引数 v に対して bool に変換可能でなければならず、値カテゴリに関係なく、v を変更してはなりません。したがって、パラメーター型 VT& は許可されていません、また VT の移動がコピーと同等でない限り VT も許可されません(C++11以降)。​

new_value - 置き換えに使用する値
型要件
-
InputItLegacyInputIterator の要件を満たす必要があります。
-
OutputItLegacyOutputIterator の要件を満たさなければなりません。
-
ForwardIt1, ForwardIt2LegacyForwardIterator の要件を満たさなければなりません。

[編集] 戻り値

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

[編集] 計算量

std::distance(first, last)N とする

1,2) N 回の operator== による比較。
3,4) 述語 pN 回の適用。

[編集] 例外

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 TCopyAssignable である必要があり (EqualityComparablereplace_copy の場合)、
しかし InputIt の値型は常に T ではありません
要件が削除されました
LWG 337 C++98 replace_copy_ifInputIt
LegacyIterator[1] の要件を満たすことのみを要求していました。
に修正されました
LegacyInputIterator
  1. C++標準の実際の欠陥は、テンプレートパラメータ InputIteratorIterator と誤って指定されていたことです。これは、C++標準がアルゴリズムライブラリの関数テンプレートについて、名前が Iterator で終わるテンプレート型パラメータが対応するイテレータカテゴリの型要件を意味すると述べているため、型要件に影響します。

[編集] 関連項目

特定の基準を満たすすべての値を別の値に置き換える
(関数テンプレート) [編集]
特定の基準を満たす要素を削除する
(関数テンプレート) [編集]
特定の基準を満たす要素を別の値に置き換えながら範囲をコピーする
(アルゴリズム関数オブジェクト)[編集]
English 日本語 中文(简体) 中文(繁體)