std::ranges::uninitialized_copy_n, std::ranges::uninitialized_copy_n_result
来自 zh.cppreference.net
| 定义于头文件 <memory>
|
||
| 调用签名 |
||
template< std::input_iterator I,
/*nothrow-input-iterator*/ O, /*nothrow-sentinel-for*/<O> S >
requires std::constructible_from<std::iter_value_t<O>,
std::iter_reference_t<I>>
uninitialized_copy_n_result<I, O>
uninitialized_copy_n( I ifirst, std::iter_difference_t<I> count,
O ofirst, S olast );
|
(1) | (C++20 起) (C++26 起 constexpr) |
template< /*execution-policy*/ Ep, std::random_access_iterator I,
/*nothrow-random-access-iterator*/ O,
/*nothrow-sized-sentinel-for*/<O> S>
requires std::constructible_from<std::iter_value_t<O>,
std::iter_reference_t<I>>
uninitialized_copy_n_result<I, O>
uninitialized_copy_n( Ep&& policy,
I ifirst, std::iter_difference_t<I> count,
O ofirst, S olast );
|
(2) | (C++26 起) |
| 辅助类型 |
||
template< class I, class O >
using uninitialized_copy_n_result = ranges::in_out_result<I, O>;
|
(3) | (C++20 起) |
关于 /*execution-policy*/ 的定义,参见此页面;关于其他仅用于阐释的概念的定义,参见此页面。
1) 从源范围
[ofirst, olast) 的中的元素构造目标范围 ifirst + [0, count) 中的元素,如同通过
auto ret = ranges::uninitialized_copy(std::counted_iterator(std::move(ifirst), count),
std::default_sentinel, ofirst, olast);
return {std::move(ret.in).base(), ret.out};
如果在初始化期间抛出异常,则已构造的对象将以未指定的顺序销毁。
如果
[ofirst, olast) 与 ifirst + [0, count) 重叠,则行为未定义。2) 同 (1),但根据
policy 执行。此页面上描述的函数式实体是 算法函数对象(非正式地称为 niebloids),这意味着:
参数
| ifirst | - | 要从中复制的范围的起始 |
| count | - | 要复制的元素数量 |
| ofirst, olast | - | 定义目标元素范围的迭代器-哨兵对 |
| policy | - | 使用的执行策略 |
返回值
如上所述。
异常
在目标范围内构造元素时抛出的任何异常。
2) 在执行过程中:
- 如果并行化所需的临时内存资源不可用,则会抛出std::bad_alloc。
- 如果在通过算法参数访问对象时抛出未被捕获的异常,则行为由执行策略决定(对于标准策略,会调用std::terminate)。
注释
如果输出范围的值类型是ranges::uninitialized_copy_n平凡类型,实现可以通过使用例如ranges::copy_n来提高的效率。
| 功能特性测试宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_parallel_algorithm |
202506L |
(C++26) | 并行范围算法 |
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr 针对 特殊化 <memory> 算法,(1)
|
可能的实现
struct uninitialized_copy_n_fn
{
template<std::input_iterator I,
/*nothrow-input-iterator*/ O, /*nothrow-sentinel-for*/<O> S>
requires std::constructible_from<std::iter_value_t<O>, std::iter_reference_t<I>>
constexpr ranges::uninitialized_copy_n_result<I, O>
operator()(I ifirst, std::iter_difference_t<I> count, O ofirst, S olast) const
{
auto iter = std::counted_iterator(std::move(ifirst), count);
auto ret = ranges::uninitialized_copy(iter, std::default_sentinel, ofirst, olast);
return {std::move(ret.in).base(), ret.out};
}
};
inline constexpr uninitialized_copy_n_fn uninitialized_copy_n{};
|
示例
运行此代码
#include <iomanip> #include <iostream> #include <memory> #include <string> int main() { const char* stars[]{"Procyon", "Spica", "Pollux", "Deneb", "Polaris"}; constexpr int n{4}; alignas(alignof(std::string)) char out[n * sizeof(std::string)]; try { auto first{reinterpret_cast<std::string*>(out)}; auto last{first + n}; auto ret{std::ranges::uninitialized_copy_n(std::begin(stars), n, first, last)}; std::cout << '{'; for (auto it{first}; it != ret.out; ++it) std::cout << (it == first ? "" : ", ") << std::quoted(*it); std::cout << "};\n"; std::ranges::destroy(first, last); } catch (...) { std::cout << "uninitialized_copy_n exception\n"; } }
输出:
{"Procyon", "Spica", "Pollux", "Deneb"};
参阅
(C++20) |
将对象范围复制到未初始化的内存区域 (算法函数对象) |
(C++11) |
将指定数量的对象复制到未初始化的内存区域 (函数模板) |