Книга: Standard Template Library Programmer

uninitialized_copy

uninitialized_copy

Categories: allocators, algorithms

Component type: function

Prototype

template <class InputIterator, class ForwardIterator>
ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);

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 [result, result + (last – first)) points to uninitialized memory, then uninitialized_copy creates a copy of [first, last) in that range. That is, for each iterator i in the input range, uninitialized_copy creates a copy of *i in the location pointed to by the corresponding iterator in the output range by calling construct(&*(result + (i – first)), *i).

Definition

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

Requirements on types

• InputIterator is a model of Input Iterator.

• ForwardIterator is a model of Forward Iterator.

• ForwardIterator is mutable.

• ForwardIterator's value type has a constructor that takes a single argument whose type is InputIterator 's value type.

Preconditions

• [first, last) is a valid range.

• [result, result + (last – first)) is a valid range.

• Each iterator in [result, result + (last – first)) 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() {
 int A1[] = {1, 2, 3, 4, 5, 6, 7};
 const int N = sizeof(A1) / sizeof(int);
 Int* A2 = (Int*) malloc(N * sizeof(Int));
 uninitialized_copy(A1, A1 + N, A2);
}

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_fill, uninitialized_fill_n, raw_storage_iterator

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


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