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

c++ stl源码剖析学习笔记(一)uninitialized_copy()函数的更多相关文章

  1. c++ stl源码剖析学习笔记(二)iterator

    ITERATOR 迭代器 template<class InputIterator,class T> InputIterator find(InputIterator first,Inpu ...

  2. c++ stl源码剖析学习笔记(三)容器 vector

    stl中容器有很多种 最简单的应该算是vector 一个空间连续的数组 他的构造函数有多个 以其中 template<typename T> vector(size_type n,cons ...

  3. STL源码剖析 学习笔记 MiniSTL

    https://github.com/joeyleeeeeee97 目录: 第二章 空间适配器 第三章 迭代器 第四章 序列式容器(vector,list,deque,stack,heap,prior ...

  4. STL源码剖析-学习笔记

    1.模板是一个公式或是蓝图,本身不是类或是函数,需进行实例化的过程.这个过程是在编译期完成的,编译器根据传递的实参,推断出形参的类型,从而实例化相应的函数 2. 后续补充-.

  5. STL源码剖析读书笔记之vector

    STL源码剖析读书笔记之vector 1.vector概述 vector是一种序列式容器,我的理解是vector就像数组.但是数组有一个很大的问题就是当我们分配 一个一定大小的数组的时候,起初也许我们 ...

  6. 重温《STL源码剖析》笔记 第三章

    源码之前,了无秘密. --侯杰 第三章:迭代器概念与traits编程技法 迭代器是一种smart pointer auto_Ptr 是一个用来包装原生指针(native pointer)的对象,声明狼 ...

  7. STL源码剖析读书笔记--第四章--序列式容器

    1.什么是序列式容器?什么是关联式容器? 书上给出的解释是,序列式容器中的元素是可序的(可理解为可以按序索引,不管这个索引是像数组一样的随机索引,还是像链表一样的顺序索引),但是元素值在索引顺序的方向 ...

  8. 重温《STL源码剖析》笔记 第六、七、八章 next_permutation (字典序)

    源码之前,了无秘密  ——侯杰 第六章算法 next_permutation 比如:01342 -> 01423 -> 01432 方法:从尾端开始往前寻找两个相邻的元素,令第一个元素为* ...

  9. 重温《STL源码剖析》笔记 第五章

    源码之前,了无秘密  ——侯杰 序列式容器 关联式容器 array(build in) RB-tree vector set heap   map priority-queue multiset li ...

随机推荐

  1. 如何在同一主机中添加多个homestead并行开发

    参考源 https://blog.csdn.net/smm188/article/details/79356150 1,在项目目录 git clone homestead 后(见上面流程中的第四步), ...

  2. 对Array.prototype.slice.call()方法的理解

    在看别人代码时,发现有这么个写法:[].slice.call(arguments, 0),这到底是什么意思呢? 1.基础 1)slice() 方法可从已有的数组中返回选定的元素. start:必需.规 ...

  3. python成功之道

    https://blog.ansheng.me/article/python-full-stack-way

  4. greendao3.2.0使用

    源代码地址 https://github.com/greenrobot/greenDAO buildscript { repositories { jcenter() mavenCentral() } ...

  5. Vue面试题

    Vue 简述下MVVM MVVM全称是MODEL-VIEW-VIEWMODEL Vue是以数据为驱动,Vue自身将DOM和数据进行绑定,一旦创建绑定,DOM和数据将保持同步,当数据发生变化,DOM也会 ...

  6. gradle-4.1-all.zip离线包下载 极速 android studio2.3 3.0编译必备

    http://download.csdn.net/download/yongheng289/10039982 gradle-4.1-all.zip离线包下载 极速 android studio2.3 ...

  7. 【Linux】【Jenkins】系统配置报反向代理设置有误问题的解决方案

    1.如图所示: 2.点击更多信息,查看解决办法: https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+says+my+reverse+proxy+s ...

  8. win10 64位,家庭版,C++,ini配置说明

      #include<windows.h> #include<iostream> #include <atlstr.h> using namespace std; ...

  9. 设置了error_reporting(E_ALL)还是不显示错误

    原因就是在php.ini里面将display_errors关闭了.可以在php.ini里面将display_errors配置为On,然后重启php-fpm ini_set('display_error ...

  10. Digital Twin的8种解读!

    国际8大主流厂商对digital twin的理解,很有必要来一次汇总! 据IDC预测,2017年世界上将有40%的大型生产商都会应用虚拟仿真技术来为他们的生产过程进行建模,Digital Twin可以 ...