Книга: Standard Template Library Programmer

uninitialized_fill

uninitialized_fill

Categories: allocators, algorithms

Component type: function

Prototype

template <class ForwardIterator, class T>
void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);

Description

In C++, the operator new allocates memory for an object and then creates an object at that location by calling a constructor. Occasionally, however, it is useful to separate those two operations. [1] If each iterator in the range [first, last) points to uninitialized memory, then uninitialized_fill creates copies of x in that range. That is, for each iterator i in the range [first, last), uninitialized_copy creates a copy of x in the location pointed to i by calling construct(&*i, x).

Definition

Defined in the standard header memory, and in the nonstandard backward-compatibility header algo.h.

Requirements on types

• ForwardIterator is a model of Forward Iterator.

• ForwardIterator is mutable.

• ForwardIterator's value type has a constructor that takes a single argument of type T.

Preconditions

• [first, last) is a valid range.

• Each iterator in [first, last) points to a region of uninitialized memory that is large enough to store a value of ForwardIterator's value type.

Complexity

Linear. Exactly last – first constructor calls.

Example

class Int {
public:
 Int(int x) : val(x) {}
 int get() { return val; }
private:
 int val;
};
int main() {
 const int N = 137;
 Int val(46);
 Int* A = (Int*) malloc(N * sizeof(Int));
 uninitialized_fill(A, A + N, val);
}

Notes

[1] In particular, this sort of low-level memory management is used in the implementation of some container classes.

See also

Allocators, construct, destroy, uninitialized_copy, uninitialized_fill_n, raw_storage_iterator

Оглавление книги


Генерация: 1.131. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз