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

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