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

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

函数使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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);

 

posted on   itdef  阅读(1665)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示