名稱空間
變體
操作

std::replace_copy, std::replace_copy_if

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (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) 替換所有謂詞 p 返回 true 的元素。
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 起)

如果表示式 *firstnew_value 的任何結果不可寫入d_first,則程式是非良構的。

如果源範圍和目標範圍重疊,則行為未定義。

目錄

[編輯] 引數

first, last - 定義要複製的元素的源 範圍 的迭代器對
d_first - 目標範圍的開頭
old_value - 要替換的元素的值
policy - 要使用的 執行策略
p - 一元謂詞,如果元素值應該被替換,則返回 true

對於型別(可能是 const)VT 的每個引數 v,表示式 p(v) 必須可轉換為 bool,其中 VTInputIt 的值型別,無論值類別如何,並且不得修改 v。因此,不允許使用 VT& 引數型別,也不允許使用 VT,除非對於 VT 移動等同於複製(C++11 起)。​

new_value - 用於替換的值
型別要求
-
InputIt 必須滿足 LegacyInputIterator 的要求。
-
OutputIt 必須滿足 LegacyOutputIterator 的要求。
-
ForwardIt1, ForwardIt2 必須滿足 LegacyForwardIterator 的要求。

[編輯] 返回值

指向最後一個被複制元素之後的元素的迭代器。

[編輯] 複雜度

給定 N 作為 std::distance(first, last)

1,2) 恰好 N 次使用 operator== 的比較。
3,4) 恰好 N 次謂詞 p 的應用。

[編輯] 異常

帶有模板引數 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++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 283 C++98 T 被要求為 可複製賦值(對於
replace_copy 還要是可相等比較),但 InputIt 的值型別並不總是 T
移除了此要求
LWG 337 C++98 replace_copy_if 僅要求 InputIt 滿足
LegacyIterator 的要求[1]
更正為
LegacyInputIterator(傳統輸入迭代器)
  1. C++ 標準中的實際缺陷是模板引數 InputIterator 被錯誤指定為 Iterator。這會影響型別要求,因為 C++ 標準規定對於演算法庫中的函式模板,其名稱以 Iterator 結尾的模板型別引數暗示了相應迭代器類別的型別要求。

[編輯] 另請參閱

用另一個值替換所有滿足特定條件的值
(函式模板) [編輯]
移除滿足特定標準的元素
(函式模板) [編輯]
複製一個範圍,同時用另一個值替換滿足特定條件的元素
(演算法函式物件)[編輯]