std::ranges::copy, std::ranges::copy_if, std::ranges::copy_result, std::ranges::copy_if_result
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
template< std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O > requires std::indirectly_copyable<I, O> |
(1) | (C++20 起) |
template< ranges::input_range R, std::weakly_incrementable O > requires std::indirectly_copyable<ranges::iterator_t<R>, O> |
(2) | (C++20 起) |
template< std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O, class Proj = std::identity, |
(3) | (C++20 起) |
template< ranges::input_range R, std::weakly_incrementable O, class Proj = std::identity, |
(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 起) |
將由 [
first,
last)
定義的範圍內的元素複製到從 result 開始的另一個範圍。
[
first,
last)
內從 first 開始併到 last - 1 結束的所有元素。如果 result 在範圍 [
first,
last)
內,則行為未定義。在這種情況下,可以改用 ranges::copy_backward。本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first, last | - | 定義要複製的元素範圍的迭代器-哨兵對 |
r | - | 要複製的元素範圍 |
result | - | 目標範圍的起始位置。 |
pred | - | 應用於投影元素的謂詞 |
proj | - | 應用於元素的投影 |
[編輯] 返回值
一個 ranges::in_out_result,包含一個等於 last 的輸入迭代器和一個指向複製的最後一個元素之後的輸出迭代器。
[編輯] 複雜度
[編輯] 注意
在實踐中,如果值型別是 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
[編輯] 參閱
(C++20) |
以逆序複製一個範圍的元素 (演算法函式物件) |
(C++20) |
建立一個反轉後的範圍副本 (演算法函式物件) |
(C++20) |
將一定數量的元素複製到一個新位置 (演算法函式物件) |
(C++20) |
給一個範圍的元素賦某個值 (演算法函式物件) |
(C++20)(C++20) |
複製一個範圍的元素,忽略那些滿足特定條件的元素 (演算法函式物件) |
(C++11) |
將一個範圍的元素複製到一個新位置 (函式模板) |