c++ stl源码剖析学习笔记(一)uninitialized_copy()函数

时间:2023-11-24 23:35:50

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

函数使用示例

#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在A2的地址上初始化了一系列Int元素,数据的拷贝来源是 A1数组

跟进源码来查看uninitialized_copy()的工作流程(源码 stl源码剖析随书代码 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));
}

主要便是uninitialized_copy()调用__uninitialized_copy() 最后一个参数是value_type(result)

value_type(result)的作用是将 ForwardIterator result 转化为指针类型T

__uninitialized_copy() 中调用__uninitialized_copy_aux() 则是判断 T是否为POD type

如何判断是否是POD type?

typedef typename __type_traits<T>::is_POD_type is_POD;

所有的基本类型都有如下定义

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;
};

而非基本类型则会对应到以下模板代码

template <class type>
struct __type_traits {
typedef __false_type is_POD_type;
};

所以当typename __type_traits<T>::is_POD_type的T 为int char double 等类型

is_POD_type为__true_type

其他类型则

is_POD_type为__false_type

如果是POD type,就是基本数据类型(int char double等)那么就直接拷贝即可

代码如下

__true_type
return copy(first, last, result);

如果不是POD type 就需要依次调用构造函数创建数据

代码如下

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