std::ranges::copy, std::ranges::copy_if, std::ranges::copy_result, std::ranges::copy_if_result
来自cppreference.com
<tbody>
</tbody>
| 在标头 <algorithm> 定义
|
||
| 调用签名 |
||
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 起) |
复制 [first, last) 所定义的范围中的元素到始于 result 的另一范围。
1) 复制范围
[first, last) 中的所有元素,从 first 开始并持续到 last - 1。若 result 在范围 [first, last) 内则行为未定义。此情况下,可用 ranges::copy_backward 代替。3) 仅复制谓词
pred 对其返回 true 的元素。保持复制的元素顺序。若源与目标范围重叠则行为未定义。2,4) 同 (1,3),但以
r 为源范围,如同以 ranges::begin(r) 为 first 并以 ranges::end(r) 为 last。此页面上描述的函数式实体是算法函数对象(非正式地称为 niebloid),即:
参数
| first, last | - | 要复制的元素范围的迭代器-哨位对 |
| r | - | 要复制的元素范围 |
| result | - | 目标范围的起始 |
| pred | - | 应用到投影后元素的谓词 |
| proj | - | 应用到元素的投影 |
返回值
含有等于 last 的输入迭代器和指向最后复制元素后一位置的输出迭代器的 ranges::in_out_result。
复杂度
1,2) 准确
(last - first) 次赋值。3,4) 准确
(last - first) 次应用谓词与投影,而赋值次数从 0 到 last - first 之间(对每个谓词返回 true 的元素赋值,取决于谓词与输入数据)。注解
实践中,ranges::copy 的实现若值类型为可平凡复制 (TriviallyCopyable) 且迭代器类型满足 contiguous_iterator,则避免多次赋值并使用如 std::memmove 的大块复制函数。
复制重叠的范围时,std::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 并显示结果 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> to_vector = from_vector;
std::cout << "目标包含: ";
std::ranges::copy(destination, std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
std::cout << "目标中的奇数有: ";
std::ranges::copy_if(destination, std::ostream_iterator<int>(std::cout, " "),
[](int x) { return (x % 2) == 1; });
std::cout << '\n';
}
输出:
目标包含: 0 1 2 3 4 5 6 7 8 9
目标中的奇数有: 1 3 5 7 9
参阅
(C++20) |
从后往前复制范围中元素 (算法函数对象) |
(C++20) |
创建范围的逆向副本 (算法函数对象) |
(C++20) |
复制若干元素到新位置 (算法函数对象) |
(C++20) |
赋给定值到范围中元素 (算法函数对象) |
(C++20)(C++20) |
复制范围并忽略满足特定条件的元素 (算法函数对象) |
(C++11) |
复制范围中元素到新位置 (函数模板) |