名稱空間
變體
操作

std::ranges::copy, std::ranges::copy_if, std::ranges::copy_result, std::ranges::copy_if_result

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

requires std::indirectly_copyable<I, O>
constexpr copy_result<I, O>

    copy( I first, S last, O result );
(1) (C++20 起)
template< ranges::input_range R, std::weakly_incrementable O >

requires std::indirectly_copyable<ranges::iterator_t<R>, O>
constexpr copy_result<ranges::borrowed_iterator_t<R>, O>

    copy( R&& r, O result );
(2) (C++20 起)
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 copy_if_result<I, O>

    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 copy_if_result<ranges::borrowed_iterator_t<R>, O>

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

將由 [firstlast) 定義的範圍內的元素複製到從 result 開始的另一個範圍。

1) 複製範圍 [firstlast) 內從 first 開始併到 last - 1 結束的所有元素。如果 result 在範圍 [firstlast) 內,則行為未定義。在這種情況下,可以改用 ranges::copy_backward
3) 僅複製謂詞 pred 返回 true 的元素。複製的元素的相對順序保持不變。如果源範圍和目標範圍重疊,則行為未定義。
2,4)(1,3) 相同,但使用 r 作為源範圍,如同使用 ranges::begin(r) 作為 firstranges::end(r) 作為 last

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

目錄

[編輯] 引數

first, last - 定義要複製的元素範圍的迭代器-哨兵對
r - 要複製的元素範圍
result - 目標範圍的起始位置。
pred - 應用於投影元素的謂詞
proj - 應用於元素的投影

[編輯] 返回值

一個 ranges::in_out_result,包含一個等於 last 的輸入迭代器和一個指向複製的最後一個元素之後的輸出迭代器。

[編輯] 複雜度

1,2) 精確地 last - first 次賦值。
3,4) 精確地 last - first 次謂詞和投影應用,以及介於 0last - first 之間的賦值(對於謂詞返回 true 的每個元素進行賦值,取決於謂詞和輸入資料)。

[編輯] 注意

在實踐中,如果值型別是 TriviallyCopyable 且迭代器型別滿足 contiguous_iterator,則 ranges::copy 的實現會避免多次賦值,並使用批次複製函式,例如 std::memmove

當複製重疊範圍時,ranges::copy 適用於向左複製(目標範圍的起始位置在源範圍之外),而 ranges::copy_backward 適用於向右複製(目標範圍的結束位置在源範圍之外)。

[編輯] 可能的實現

copy (1)(2)
struct copy_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O>
    requires std::indirectly_copyable<I, O>
    constexpr ranges::copy_result<I, O> operator()(I first, S last, O result) const
    {
        for (; first != last; ++first, (void)++result)
            *result = *first;
        return {std::move(first), std::move(result)};
    }
 
    template<ranges::input_range R, std::weakly_incrementable O>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O>
    constexpr ranges::copy_result<ranges::borrowed_iterator_t<R>, O>
        operator()(R&& r, O result) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(result));
    }
};
 
inline constexpr copy_fn copy;
copy_if (3)(4)
struct 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::copy_if_result<I, O>
        operator()(I first, S last, O result, Pred pred, Proj proj = {}) const
    {
        for (; first != last; ++first)
            if (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::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::ref(pred), std::ref(proj));
    }
};
 
inline constexpr copy_if_fn copy_if;

[編輯] 示例

以下程式碼使用 ranges::copy 複製一個 std::vector 的內容到另一個,並顯示結果的 std::vector

#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
 
int main()
{
    std::vector<int> source(10);
    std::iota(source.begin(), source.end(), 0);
    std::vector<int> destination;
    std::ranges::copy(source.begin(), source.end(), std::back_inserter(destination));
 
// or, alternatively,
//  std::vector<int> destination(source.size());
//  std::ranges::copy(source.begin(), source.end(), destination.begin());
// either way is equivalent to
//  std::vector<int> destination = source;
 
    std::cout << "Destination contains: ";
    std::ranges::copy(destination, std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
 
    std::cout << "Odd numbers in destination are: ";
    std::ranges::copy_if(destination, std::ostream_iterator<int>(std::cout, " "),
                         [](int x) { return (x % 2) == 1; });
    std::cout << '\n';
}

輸出

Destination contains: 0 1 2 3 4 5 6 7 8 9
Odd numbers in destination are: 1 3 5 7 9

[編輯] 參閱

以逆序複製一個範圍的元素
(演算法函式物件)[編輯]
建立一個反轉後的範圍副本
(演算法函式物件)[編輯]
將一定數量的元素複製到一個新位置
(演算法函式物件)[編輯]
給一個範圍的元素賦某個值
(演算法函式物件)[編輯]
複製一個範圍的元素,忽略那些滿足特定條件的元素
(演算法函式物件)[編輯]
將一個範圍的元素複製到一個新位置
(函式模板) [編輯]