Memory Basic processing tools:
The STL has five global functions that are used for uninitialized space, namely: construct (), Destroy (), Uninialized_copy (), Uninialized_fill (), and Uninialized_fill_n ().
The first two functions are described in the previous article and the following three functions are described below.
Uninitialized_copy () allows us to isolate the configuration of the memory from the construction behavior of the object. If every iterator in the range of [result, result+ (Last-first)) in the output destination points to an uninitialized zone, Uninitialized_copy () uses copy construct as the input source [first, Last) each object within the range produces a copy that is placed into the output range.
This function is useful if you need to initialize a container. This is done in two steps:
1) Configure memory chunks to contain all the elements in the range;
2) Use Uninitizlied_copy to construct elements on this memory block.
The C + + standard also stipulates that Uninitialized_copy () has a "commit or rollback" function. Either constructs all elements, or constructs nothing, and cannot exist in a semi-constructed state.
Within Uninitialized_copy (), an internal implementation function __uninitialized_copy () is invoked to extract the type of the iterator's pointer to the object using the compiler's type derivation function. Look at the code:
Template <classInputiterator,classForwarditerator>inline ForwardIterator __uninitialized_copy_aux (inputiterator First, Inputiterator Last, ForwardIteratorresult, __true_type) {return Copy( First, Last,result);} Template <classInputiterator,classForwarditerator>forwarditerator __uninitialized_copy_aux (inputiterator First, Inputiterator Last, ForwardIteratorresult, __false_type) {ForwardIterator cur =result; __stl_try { for( ; First!= Last; ++ First, ++cur) construct (&*cur, * First);returnCur } __stl_unwind (Destroy (result, cur));} Template <classInputiterator,classForwardIterator,classT>inline Forwarditerator__uninitialized_copy (inputiterator First, Inputiterator Last, ForwardIteratorresult, t*) {typedef typename __type_traits<t>::is_pod_type is_pod;return__uninitialized_copy_aux ( First, Last,result, Is_pod ());} Template <classInputiterator,classForwarditerator>inline ForwardIterator uninitialized_copy (inputiterator First, Inputiterator Last, ForwardIteratorresult) {return__uninitialized_copy ( First, Last,result, Value_type (result));} Inline char* uninitialized_copy (const char* First, const char* Last, char*result) {Memmove (result, First, Last- First);return result+ ( Last- First);} Inline wchar_t* uninitialized_copy (const wchar_t* First, const wchar_t* Last, wchar_t*result) {Memmove (result, First, sizeof (wchar_t) * ( Last- First));return result+ ( Last- First);}
The pod here means Plain old Data, the scalar type (scalar types) or the traditional C struct type. The POD type must have the trivial ctor/dtor/copy/assignment function.
Uninitialized_fill (), Uninitialized_copy (), Uninitialized_fill_n () almost, extrapolate. All three have "commit or rollback" semantics.
Uninitialized_fill:
Template <classForwardIterator,classT>inline Void__uninitialized_fill_aux (ForwardIterator First, ForwardIterator Last, const t& x, __true_type) {Fill ( First, Last, x);} Template <classForwardIterator,classT>void__uninitialized_fill_aux (ForwardIterator First, ForwardIterator Last, const t& x, __false_type) {ForwardIterator cur = First; __stl_try { for(; Cur! = Last; ++cur) construct (&*cur, x); } __stl_unwind (Destroy ( First, cur));} Template <classForwardIterator,classTclasst1>inline void __uninitialized_fill (ForwardIterator First, ForwardIterator Last, const t& x, t1*) {typedef typename __type_traits<t1>::is_pod_type is_pod; __uninitialized_fill_aux ( First, Last, X, Is_pod ());} Template <classForwardIterator,classt>inline void Uninitialized_fill (ForwardIterator First, ForwardIterator Last, const t& x) {__uninitialized_fill ( First, Last, X, Value_type ( First));}
Uninitialized_fill_n:
Template<classForwardIterator,classSize,classT>inlineForwarditerator__uninitialized_fill_n_aux (forwarditerator First, Size N,Constt& x, __true_type) {returnFill_n (First, n, x);}Template<classForwardIterator,classSize,classT>forwarditerator__uninitialized_fill_n_aux (forwarditerator First, Size N,Constt& x, __false_type) {forwarditerator cur = first; __stl_try { for(; n >0; --n, ++cur) construct (&*cur, x);returnCur } __stl_unwind (Destroy (first, cur));}Template<classForwardIterator,classSize,classTclassT1>inlineForwardIterator __uninitialized_fill_n (forwarditerator First, Size N,Constt& x, t1*) {typedef TypeName__type_traits<t1>::is_pod_type is_pod;return__uninitialized_fill_n_aux (First, n, X, Is_pod ());}Template<classForwardIterator,classSize,classT>inlineForwardIterator Uninitialized_fill_n (forwarditerator First, Size N,Constt& x) {return__uninitialized_fill_n (First, n, X, Value_type (first));}
STL Source Analysis (2): Memory Basic processing Tool