std::ranges:: copy, std::ranges:: copy_if, std::ranges:: copy_result, std::ranges:: copy_if_result
|
定义于头文件
<algorithm>
|
||
|
调用签名
|
||
|
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 的输入迭代器和一个指向最后被复制元素之后位置的输出迭代器。
复杂度
注释
在实际应用中,
ranges::copy
的实现会避免多次赋值操作,当值类型满足
TriviallyCopyable
且迭代器类型满足
contiguous_iterator
时,会使用批量拷贝函数(如
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)); // 或者也可以这样写: // std::vector<int> destination(source.size()); // std::ranges::copy(source.begin(), source.end(), destination.begin()); // 两种写法都等价于: // 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)
|
复制一系列元素到新位置
(函数模板) |