名稱空間
變體
操作

std::ranges::remove_copy, std::ranges::remove_copy_if, std::ranges::remove_copy_result, std::ranges::remove_copy_if_result

來自 cppreference.com
< cpp‎ | 演算法‎ | 範圍
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
受約束演算法
此選單中的所有名稱均屬於名稱空間 std::ranges
非修改序列操作
修改序列操作
劃分操作
排序操作
二分查詢操作(在已排序的範圍內)
       
       
集合操作(於已排序範圍上)
堆操作
最小/最大值操作
       
       
排列操作
摺疊操作
數值操作
(C++23)            
對未初始化儲存的操作
返回型別
 
定義於標頭檔案 <algorithm>
呼叫簽名 (Call signature)
(1)
template< std::input_iterator I, std::sentinel_for<I> S,

          std::weakly_incrementable O, class T, class Proj = std::identity >
requires std::indirectly_copyable<I, O> &&
         std::indirect_binary_predicate
             <ranges::equal_to, std::projected<I, Proj>, const T*>
constexpr remove_copy_result<I, O>

    remove_copy( I first, S last, O result, const T& value, Proj proj = {} );
(C++20 起)
(直到 C++26)
template< std::input_iterator I, std::sentinel_for<I> S,

          std::weakly_incrementable O, class Proj = std::identity,
          class T = std::projected_value_t<I, Proj> >
requires std::indirectly_copyable<I, O> &&
         std::indirect_binary_predicate
             <ranges::equal_to, std::projected<I, Proj>, const T*>
constexpr remove_copy_result<I, O>

    remove_copy( I first, S last, O result, const T& value, Proj proj = {} );
(C++26 起)
(2)
template< ranges::input_range R,

          std::weakly_incrementable O, class T, class Proj = std::identity >
requires std::indirectly_copyable<ranges::iterator_t<R>, O> &&
         std::indirect_binary_predicate
             <ranges::equal_to,
              std::projected<ranges::iterator_t<R>, Proj>, const T*>
constexpr remove_copy_result<ranges::borrowed_iterator_t<R>, O>

    remove_copy( R&& r, O result, const T& value, Proj proj = {} );
(C++20 起)
(直到 C++26)
template< ranges::input_range R,

          std::weakly_incrementable O, class Proj = std::identity,
          class T = std::projected_value_t<ranges::iterator_t<R>, Proj> >
requires std::indirectly_copyable<ranges::iterator_t<R>, O> &&
         std::indirect_binary_predicate
             <ranges::equal_to,
              std::projected<ranges::iterator_t<R>, Proj>, const T*>
constexpr remove_copy_result<ranges::borrowed_iterator_t<R>, O>

    remove_copy( R&& r, O result, const T& value, Proj proj = {} );
(C++26 起)
template< std::input_iterator I, std::sentinel_for<I> S,

          std::weakly_incrementable O, class Proj = std::identity,
          std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
requires std::indirectly_copyable<I, O>
constexpr remove_copy_if_result<I, O>

    remove_copy_if( I first, S last, O result, Pred pred, Proj proj = {} );
(3) (C++20 起)
template< ranges::input_range R,

          std::weakly_incrementable O, class Proj = std::identity,
          std::indirect_unary_predicate<
              std::projected<ranges::iterator_t<R>, Proj>> Pred >
requires std::indirectly_copyable<ranges::iterator_t<R>, O>
constexpr remove_copy_if_result<ranges::borrowed_iterator_t<R>, O>

    remove_copy_if( R&& r, O result, Pred pred, Proj proj = {} );
(4) (C++20 起)
輔助型別
template< class I, class O >
using remove_copy_result = ranges::in_out_result<I, O>;
(5) (C++20 起)
template< class I, class O >
using remove_copy_if_result = ranges::in_out_result<I, O>;
(6) (C++20 起)

將元素從源範圍 [firstlast) 複製到始於 result 的目標範圍,同時忽略(在透過 proj 投影后)滿足特定條件的元素。如果源範圍和目標範圍重疊,則行為未定義。

1) 忽略所有等於 value 的元素。
3) 忽略所有謂詞 pred 返回 true 的元素。
2,4)(1,3) 相同,但使用 r 作為源範圍,如同使用 ranges::begin(r) 作為 first,並使用 ranges::end(r) 作為 last

本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即

  • 呼叫它們中的任何一個時,不能指定顯式模板引數列表。
  • 它們中的任何一個都對 引數依賴查詢 不可見。
  • 當透過非限定查詢在函式呼叫運算子的左側找到其中任何一個作為名稱時,引數依賴查詢被抑制。

目錄

[編輯] 引數

first, last - 定義要處理的元素源範圍的迭代器-哨兵對
r - 元素的源範圍
result - 目標範圍的開頭
value - 複製的元素的值
comp - 用於比較投影元素的二元謂詞
proj - 應用於元素的投影

[編輯] 返回值

{last, result + N},其中 N 是複製的元素數量。

[編輯] 複雜度

對應謂詞 comp 和任何投影 proj 的精確 ranges::distance(first, last) 次應用。

[編輯] 注意

該演算法是穩定的,即保留了複製元素的相對順序。

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

[編輯] 可能實現

remove_copy (1,2)
struct remove_copy_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S,
             std::weakly_incrementable O, class Proj = std::identity,
             class T = std::projected_value_t<I, Proj>>
    requires std::indirectly_copyable<I, O> &&
             std::indirect_binary_predicate<ranges::equal_to,
                                            std::projected<I, Proj>, const T*>
    constexpr ranges::remove_copy_result<I, O>
        operator()(I first, S last, O result, const T& value, Proj proj = {}) const
    {
        for (; !(first == last); ++first)
            if (value != std::invoke(proj, *first))
            {
                *result = *first;
                ++result;
            }
        return {std::move(first), std::move(result)};
    }
 
    template<ranges::input_range R, 
             std::weakly_incrementable O, class Proj = std::identity,
             class T = std::projected_value_t<ranges::iterator_t<R>, Proj>>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O> &&
             std::indirect_binary_predicate<ranges::equal_to,
             std::projected<ranges::iterator_t<R>, Proj>, const T*>
    constexpr ranges::remove_copy_result<ranges::borrowed_iterator_t<R>, O>
        operator()(R&& r, O result, const T& value, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(result), value,
                       std::move(proj));
    }
};
 
inline constexpr remove_copy_fn remove_copy {};
remove_copy_if (3,4)
struct remove_copy_if_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O,
             class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    requires std::indirectly_copyable<I, O>
    constexpr ranges::remove_copy_if_result<I, O>
        operator()(I first, S last, O result, Pred pred, Proj proj = {}) const
    {
        for (; first != last; ++first)
            if (false == std::invoke(pred, std::invoke(proj, *first)))
            {
                *result = *first;
                ++result;
            }
        return {std::move(first), std::move(result)};
    }
 
    template<ranges::input_range R, std::weakly_incrementable O,
             class Proj = std::identity,
             std::indirect_unary_predicate<
                 std::projected<ranges::iterator_t<R>, Proj>> Pred>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O>
    constexpr ranges::remove_copy_if_result<ranges::borrowed_iterator_t<R>, O>
        operator()(R&& r, O result, Pred pred, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(result),
                       std::move(pred), std::move(proj));
    }
};
 
inline constexpr remove_copy_if_fn remove_copy_if {};

[編輯] 示例

#include <algorithm>
#include <array>
#include <complex>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string_view>
#include <vector>
 
void println(const auto rem, const auto& v)
{
    std::cout << rem << ' ';
    for (const auto& e : v)
        std::cout << e << ' ';
    std::cout << '\n';
}
 
int main()
{
    // Filter out the hash symbol from the given string.
    const std::string_view str{"#Small #Buffer #Optimization"};
    std::cout << "before: " << std::quoted(str) << '\n';
 
    std::cout << "after:  \"";
    std::ranges::remove_copy(str.begin(), str.end(),
                             std::ostream_iterator<char>(std::cout), '#');
    std::cout << "\"\n";
 
    // Copy only the complex numbers with positive imaginary part.
    using Ci = std::complex<int>;
    constexpr std::array<Ci, 5> source
    {
        Ci{1, 0}, Ci{0, 1}, Ci{2, -1}, Ci{3, 2}, Ci{4, -3}
    };
    std::vector<std::complex<int>> target;
 
    std::ranges::remove_copy_if
    (
        source,
        std::back_inserter(target),
        [](int imag) { return imag <= 0; },
        [](Ci z) { return z.imag(); }
    );
 
    println("source:", source);
    println("target:", target);
 
    std::vector<std::complex<float>> nums{{2, 2}, {1, 3}, {4, 8}, {1, 3}};
    std::vector<std::complex<double>> outs;
    #ifdef __cpp_lib_algorithm_default_value_type
        std::remove_copy(nums.cbegin(), nums.cend(), std::back_inserter(outs),
                         {1, 3}); // T gets deduced to std::complex<float>
    #else
        std::remove_copy(nums.cbegin(), nums.cend(), std::back_inserter(outs),
                         std::complex<float>{1, 3});
    #endif
    println("nums:  ", nums);
    println("outs:  ", outs);
}

輸出

before: "#Small #Buffer #Optimization"
after:  "Small Buffer Optimization"
source: (1,0) (0,1) (2,-1) (3,2) (4,-3)
target: (0,1) (3,2)
nums:   (2,2) (1,3) (4,8) (1,3)
outs:   (2,2) (4,8)

[編輯] 參閱

移除滿足特定標準的元素
(演算法函式物件)[編輯]
將一個範圍的元素複製到一個新位置
(演算法函式物件)[編輯]
將一定數量的元素複製到一個新位置
(演算法函式物件)[編輯]
以逆序複製一個範圍的元素
(演算法函式物件)[編輯]
複製一個範圍,同時用另一個值替換滿足特定條件的元素
(演算法函式物件)[編輯]
建立一個反轉後的範圍副本
(演算法函式物件)[編輯]
複製並旋轉一個範圍的元素
(演算法函式物件)[編輯]
建立一個不含連續重複元素的某個元素範圍的副本
(演算法函式物件)[編輯]
複製一個範圍的元素,忽略那些滿足特定條件的元素
(函式模板) [編輯]