定义于头文件
std::uninitialized_copy
| template< class InputIt, class ForwardIt > | (1) | |
| template< class ExecutionPolicy, class InputIt, class ForwardIt > | (2) | (C++17 起) |
1) 复制来自范围 [first, last) 的元素到始于 d_first 的未初始化内存,如同用
for (; first != last; ++d_first, (void) ++first) ::new (static_cast(std::addressof(*d_first))) typename std::iterator_traits ::value_type(*first);
若初始化中抛异常,则以未指定顺序销毁已构造的对象。
2) 同 (1) ,但按照 policy 执行。此重载仅若 std::is_execution_policy_vtrue 才参与重载决议。
| first, last | - | 要复制的元素范围 |
| d_first | - | 目标范围的起始 |
| policy | - | 所用的执行策略。细节见执行策略。 |
| 类型要求 | ||
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。 | ||
- ForwardIt 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。 | ||
- 通过ForwardIt 合法实例的自增、赋值、比较或间接均不可抛异常。 | ||
指向最后复制的元素后一元素的迭代器。
与 first 和 last 间的距离成线性
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
ExecutionPolicy 为标准策略之一,则调用 std::terminate 。对于任何其他 ExecutionPolicy ,行为是实现定义的。- template<class InputIt, class ForwardIt>
- ForwardIt uninitialized_copy(InputIt first, InputIt last, ForwardIt d_first)
- {
- typedef typename std::iterator_traits<ForwardIt>::value_type Value;
- ForwardIt current = d_first;
- try {
- for (; first != last; ++first, (void) ++current) {
- ::new (static_cast<void*>(std::addressof(*current))) Value(*first);
- }
- return current;
- } catch (...) {
- for (; d_first != current; ++d_first) {
- d_first->~Value();
- }
- throw;
- }
- }
- #include <iostream>
- #include <vector>
-
- struct Foo
- {
- int N;
- Foo() {}
- explicit Foo(int n): N(n)
- {
- std::cout << "Foo(n)" << std::endl;
- }
-
- Foo(const Foo & o)
- {
- std::cout << "Foo(const Foo & o)" << std::endl;
- this->N = o.N;
- }
-
- Foo & operator =(const Foo &o)
- {
- std::cout << "operator =" << std::endl;
- this->N = o.N;
- return *this;
- }
-
- };
-
- int main()
- {
- std::vector<Foo> from_vector{Foo(1), Foo(2), Foo(3), Foo(4), Foo(5)};
- std::vector<Foo> to_vector(from_vector.size());
- std::uninitialized_copy(from_vector.begin(), from_vector.end(), to_vector.begin());
- std::cout << "to_vector size: " << to_vector.size() << std::endl;
- for (const Foo & o : to_vector)
- {
- std::cout << "Foo: " << o.N << std::endl;
- }
- }

std::uninitialized_copy_n
| template< class InputIt, class Size, class ForwardIt > | (1) | (C++11 起) |
| template< class ExecutionPolicy, class InputIt, class Size, class ForwardIt > | (2) | (C++17 起) |
1) 从始于 first 的范围复制 count 个元素到始于 d_first 的未初始化内存区域,如同以
for ( ; n > 0; ++d_first, (void) ++first, --n) ::new (static_cast(std::addressof(*d_first))) typename std::iterator_traits ::value_type(*first);
若初始化中抛异常,则以未指定顺序销毁已构造的对象。
2) 同 (1) ,但按照 policy 执行。此重载仅若 std::is_execution_policy_vtrue 才参与重载决议。
| first | - | 要复制的元素范围起始 |
| d_first | - | 目标范围起始 |
| policy | - | 所用的执行策略。细节见执行策略。 |
| 类型要求 | ||
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。 | ||
- ForwardIt 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。 | ||
- 通过ForwardIt 合法实例的自增、赋值、比较或间接均不可抛异常。 | ||
指向最后复制的元素后一元素的迭代器。
与 count 成线性。
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
ExecutionPolicy 为标准策略之一,则调用 std::terminate 。对于任何其他 ExecutionPolicy ,行为是实现定义的。- template<class InputIt, class Size, class ForwardIt>
- ForwardIt uninitialized_copy_n(InputIt first, Size count, ForwardIt d_first)
- {
- typedef typename std::iterator_traits<ForwardIt>::value_type Value;
- ForwardIt current = d_first;
- try {
- for (; count > 0; ++first, (void) ++current, --count) {
- ::new (static_cast<void*>(std::addressof(*current))) Value(*first);
- }
- } catch (...) {
- for (; d_first != current; ++d_first) {
- d_first->~Value();
- }
- throw;
- }
- return current;
- }
- #include <iostream>
- #include <vector>
-
- struct Foo
- {
- int N;
- Foo() {}
- explicit Foo(int n): N(n)
- {
- std::cout << "Foo(n)" << std::endl;
- }
-
- Foo(const Foo & o)
- {
- std::cout << "Foo(const Foo & o)" << std::endl;
- this->N = o.N;
- }
-
- Foo & operator =(const Foo &o)
- {
- std::cout << "operator =" << std::endl;
- this->N = o.N;
- return *this;
- }
-
- };
-
- int main()
- {
- std::vector<Foo> from_vector{Foo(1), Foo(2), Foo(3), Foo(4), Foo(5)};
- std::vector<Foo> to_vector(from_vector.size());
- std::uninitialized_copy_n(from_vector.begin(), 3, to_vector.begin());
- std::cout << "to_vector size: " << to_vector.size() << std::endl;
- for (const Foo & o : to_vector)
- {
- std::cout << "Foo: " << o.N << std::endl;
- }
- }
