C + + STL Source Analysis Learning Notes (i)

Source: Internet
Author: User

Template <class Inputiterator, class forwarditerator>
Inline ForwardIterator uninitialized_copy (inputiterator First, inputiterator last,forwarditerator result)

Function Use Example

#include <algorithm> #include <iostream> #include <memory> #include <string> #include <tuple > #include <vector>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));     Std::uninitialized_copy (A1,A1+N,A2);     for (int i = 0; i < N; ++i) {         std::cout << a2[i].get () << "";     }}

Uninitialized_copy Initializes a series of int elements at A2 's address, where the data is copied from the A1 array

Follow-up source to view Uninitialized_copy () workflow (source code STL source analysis with the book Codes Tass-sgi-stl-2.91.57-source)

Template <class Inputiterator, class forwarditerator>
Inline ForwardIterator uninitialized_copy (inputiterator First, inputiterator last, forwarditerator result)

--->

__uninitialized_copy (First, last, result, value_type (result));

--->

__uninitialized_copy_aux (First, last, result, is_pod ());

--->

Template <class Inputiterator, class forwarditerator>
Inline ForwardIterator
__uninitialized_copy_aux (inputiterator First, Inputiterator last,
ForwardIterator result,
__true_type) {
Return copy (First, last, result);
}

Template <class Inputiterator, class forwarditerator>
ForwardIterator
__uninitialized_copy_aux (inputiterator First, Inputiterator last,
ForwardIterator result,
__false_type) {
ForwardIterator cur = result;
__stl_try {
for (; first! = last; ++first, ++cur)
Construct (&*cur, *first);
return cur;
}
__stl_unwind (Destroy (result, cur));
}

The main thing is uninitialized_copy () call __uninitialized_copy () The last argument is value_type (result)

Value_type (Result) is the function of converting forwarditerator result to pointer type T

Call __uninitialized_copy_aux () in __uninitialized_copy () is to determine if T is pod type

How do I tell if it is pod type?

typedef typename __TYPE_TRAITS<T>::IS_POD_TYPE Is_pod;

All the basic types are defined as follows

struct __type_traits<char> {
typedef __true_type IS_POD_TYPE;
};

struct __type_traits<signed char> {
typedef __true_type IS_POD_TYPE;
};

struct __type_traits<int> {
typedef __true_type IS_POD_TYPE;
};

Instead of the base type, it corresponds to the following template code

Template <class type>
struct __type_traits {
typedef __false_type IS_POD_TYPE;
};

So when TypeName __type_traits<t>::is_pod_type's T is int char double type

Is_pod_type to __true_type

Other types are

Is_pod_type to __false_type

If it is pod type, it is the basic data type (int char double, etc.) then copy directly

The code is as follows

__true_type
Return copy (First, last, result);

If it is not pod type, you need to call the constructor in turn to create the data

The code is as follows

__false_type
for (; first! = last; ++first, ++cur)
Construct (&*cur, *first);

C + + STL Source Analysis Learning Note (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.