Int* A2 = (Int*)malloc(N * sizeof(Int));
uninitialized_copy_n(A1, N, A2);
}
Notes
[1] In particular, this sort of low-level memory management is used in the implementation of some container classes.
[2] Uninitialized_copy_n is almost, but not quite, redundant. If first is an input iterator, as opposed to a forward iterator, then the uninitialized_copy_n operation can't be expressed in terms of uninitialized_copy .
See also
Allocators, construct , destroy , uninitialized_copy , uninitialized_fill , uninitialized_fill_n , raw_storage_iterator
Categories: allocators, algorithms
Component type: function
Prototype
template
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
Categories: allocators, algorithms
Component type: function
Prototype
template
ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n, 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, first + n) points to uninitialized memory, then uninitialized_fill_n creates copies of x in that range. That is, for each iterator i in the range [first, first + n) , uninitialized_fill_n 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.
• Size is an integral type that is convertible to ForwardIterator 's distance type.
• ForwardIterator 's value type has a constructor that takes a single argument of type T .
Preconditions
• n is nonnegative.
• [first, first + n) is a valid range.
• Each iterator in [first, first + n) points to a region of uninitialized memory that is large enough to store a value of ForwardIterator 's value type.
Complexity
Linear. Exactly n 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_n(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 , raw_storage_iterator
Category: allocators
Component type: type
Description
Some algorithms, such as stable_sort and inplace_merge , are adaptive : they attempt to use extra temporary memory to store intermediate results, and their run-time complexity is better if that extra memory is available. These algorithms use temporary_buffer to allocate that extra memory.
temporary_buffer 's constructor takes two arguments, first and last , of type ForwardIterator ; the constructor allocates a buffer that is large enough to contain N objects of type T , where 0 <= N <= last – first [1], and it fills the buffer with objects of type T . The member functions begin() and end() return iterators that point to the beginning and the end of the buffer.
Note that the elements in the buffer are guaranteed to be initialized; that is, begin() points to an object of type T , not to raw memory. However, the initial values of the buffer's elements are unspecified. You should not rely on them to be initialized to any particular value.
temporary_buffer does not have a copy constructor, or an assignment operator. Those operations would have complicated, and not terribly useful, semantics.
Читать дальше