c++ stl源码剖析学习笔记(一)uninitialized_copy()函数
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()函数的更多相关文章
- c++ stl源码剖析学习笔记(二)iterator
ITERATOR 迭代器 template<class InputIterator,class T> InputIterator find(InputIterator first,Inpu ...
- c++ stl源码剖析学习笔记(三)容器 vector
stl中容器有很多种 最简单的应该算是vector 一个空间连续的数组 他的构造函数有多个 以其中 template<typename T> vector(size_type n,cons ...
- STL源码剖析 学习笔记 MiniSTL
https://github.com/joeyleeeeeee97 目录: 第二章 空间适配器 第三章 迭代器 第四章 序列式容器(vector,list,deque,stack,heap,prior ...
- STL源码剖析-学习笔记
1.模板是一个公式或是蓝图,本身不是类或是函数,需进行实例化的过程.这个过程是在编译期完成的,编译器根据传递的实参,推断出形参的类型,从而实例化相应的函数 2. 后续补充-.
- STL源码剖析读书笔记之vector
STL源码剖析读书笔记之vector 1.vector概述 vector是一种序列式容器,我的理解是vector就像数组.但是数组有一个很大的问题就是当我们分配 一个一定大小的数组的时候,起初也许我们 ...
- 重温《STL源码剖析》笔记 第三章
源码之前,了无秘密. --侯杰 第三章:迭代器概念与traits编程技法 迭代器是一种smart pointer auto_Ptr 是一个用来包装原生指针(native pointer)的对象,声明狼 ...
- STL源码剖析读书笔记--第四章--序列式容器
1.什么是序列式容器?什么是关联式容器? 书上给出的解释是,序列式容器中的元素是可序的(可理解为可以按序索引,不管这个索引是像数组一样的随机索引,还是像链表一样的顺序索引),但是元素值在索引顺序的方向 ...
- 重温《STL源码剖析》笔记 第六、七、八章 next_permutation (字典序)
源码之前,了无秘密 ——侯杰 第六章算法 next_permutation 比如:01342 -> 01423 -> 01432 方法:从尾端开始往前寻找两个相邻的元素,令第一个元素为* ...
- 重温《STL源码剖析》笔记 第五章
源码之前,了无秘密 ——侯杰 序列式容器 关联式容器 array(build in) RB-tree vector set heap map priority-queue multiset li ...
随机推荐
- 利用 Chrome 原生功能截图网页全图
打开你想截图的网页了,然后按下 F12(macOS 是 option + command + i)调出开发者工具,接着按「Ctrl + Shift + P」(macOS 是 command + Shi ...
- MYSQL ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.10.210' (111) 解决方法
今天在测试MySQL的连接时候,发现连接不通过,并报错ERROR 2003 (HY000): Can't connect to mysql server on '192.168.10.210' (11 ...
- c# 泛型和IComparable<T>接口
泛型 因为我们在编程中想先不定义数据类型,只想先写逻辑,就可以使用Object类型, 这样我们的逻辑就适用于所有类型,但是,在运行中,Object类型的变量会需要 转换到对应类型,浪费资源,所有出现泛 ...
- WPF DEV dxc:ChartControl 柱状图
先上效果图: <UserControl xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" mc ...
- Titanic缺失数值处理 & 存活率预测
1. kaggle泰坦尼克数据titanic完整下载,原作者良心分享 https://download.csdn.net/download/lansui7312/9936840 2. 缺失值处理 # ...
- <记录> HtmlHelper和 强类型页面
HtmlHelper 路径生成 <!--普通写法--> <a href="/home/index">超链接</a> <!--利用Url类 ...
- Rabbitmq(7) confirm模式
1. //将通道设置为comfirm模式channel.confirmSelect(); // 消息确认if(!channel.waitForConfirms()){ System.out.print ...
- Garbage Disposal(模拟垃圾装垃圾口袋)
Garbage Disposal Description Enough is enough. Too many times it happened that Vasya forgot to dispo ...
- 基于windows平台搭建elasticsearch
部署准备 elasticsearch-6.0.1.zip--https://www.elastic.co/downloads/elasticsearch elasticsearch-head-mast ...
- docker 部署tomcat
使用Docker搭建Tomcat运行环境 1 准备宿主系统 准备一个 CentOS 7操作系统,具体要求如下: 必须是 64 位操作系统 建议内核在 3.8 以上 通过以下命令查看您的 CentOS ...