名稱空間
變體
操作

std::remove_copy, std::remove_copy_if

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
定義於標頭檔案 <algorithm>
(1)
template< class InputIt, class OutputIt, class T >

OutputIt remove_copy( InputIt first, InputIt last,

                      OutputIt d_first, const T& value );
(C++20 起為 constexpr)
(直到 C++26)
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 );
(C++26 起)
(2)
template< class ExecutionPolicy,

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

                        ForwardIt2 d_first, const T& value );
(C++17 起)
(直到 C++26)
template< class ExecutionPolicy,

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

                        ForwardIt2 d_first, const T& value );
(C++26 起)
template< class InputIt, class OutputIt, class UnaryPred >

OutputIt remove_copy_if( InputIt first, InputIt last,

                         OutputIt d_first, UnaryPred p );
(3) (C++20 起為 constexpr)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2, class UnaryPred >
ForwardIt2 remove_copy_if( ExecutionPolicy&& policy,
                           ForwardIt1 first, ForwardIt1 last,

                           ForwardIt2 d_first, UnaryPred p );
(4) (C++17 起)

將範圍 [firstlast) 中的元素複製到從 d_first 開始的另一個範圍,同時省略滿足特定條件的元素。

1) 忽略所有等於 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 起)

如果 *d_first = *first 無效(C++20 前)*first 不可寫入d_first(C++20 起),則程式格式錯誤。

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

目錄

[編輯] 引數

first, last - 定義要複製的元素的源 範圍 的迭代器對
d_first - 目標範圍的開頭
value - 不復制的元素的值
policy - 要使用的 執行策略
型別要求
-
InputIt 必須滿足 LegacyInputIterator 的要求。
-
OutputIt 必須滿足 LegacyOutputIterator 的要求。
-
ForwardIt1, ForwardIt2 必須滿足 LegacyForwardIterator 的要求。
-
UnaryPred 必須滿足 Predicate 的要求。

[編輯] 返回值

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

[編輯] 複雜度

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

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

對於帶有 ExecutionPolicy 的過載,如果 ForwardIt1value_type 不可 MoveConstructible,可能會有效能開銷。

[編輯] 異常

帶有模板引數 ExecutionPolicy 的過載按如下方式報告錯誤

  • 如果作為演算法一部分呼叫的函式執行丟擲異常,並且 ExecutionPolicy標準策略 之一,則呼叫 std::terminate。對於任何其他 ExecutionPolicy,行為由實現定義。
  • 如果演算法未能分配記憶體,則丟擲 std::bad_alloc

[編輯] 可能的實現

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;
}

[編輯] 注意

特性測試 標準 特性
__cpp_lib_algorithm_default_value_type 202403 (C++26) 演算法的列表初始化 (1,2)

[編輯] 示例

#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)

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 779 C++98 要求 TEqualityComparable,但
ForwardIt 的值型別不總是 T
要求 *d_first = *first
是有效的,而不是

[編輯] 參閱

移除滿足特定標準的元素
(函式模板) [編輯]
將一個範圍的元素複製到一個新位置
(函式模板) [編輯]
複製一個範圍,並將元素分成兩組
(函式模板) [編輯]
複製一個範圍的元素,忽略那些滿足特定條件的元素
(演算法函式物件)[編輯]