本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

copy

//唯一对外接口
/*--------------------------------------------------------------------------------------
* copy 函数及其重载形式
*/
//全然泛化版本号。
template<class InputIterator, class OutputIterator> // ? 这里的 InputIterator 和 OutputIterator 都仅仅是名称而已,哪里确保了它们真的至少是 InputIterator 和 OutputIterator ?
inline OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result){
// 不明确为什么要用 function object 实现,用普通的 function 实现不行吗?
// 由于 function object 能够偏特化 ? 但普通的 function 也能够重载呀
// 难道由于通过 function object 实现的偏特化是编译时多态。比 function 重载的执行时多态要好 ?
return __copy_dispatch<InputIterator, OutputIterator>() (first, last, result);
} //针对原生指针(可视为一种特殊的迭代器) const char * 和 const wchar_t *, 进行内存直接拷贝操作
//特殊版本号[1] 重载形式
inline char *copy(const char* first, const char *last, char *result){
memmove(result, first, last - first);
return result + (last - first); //为什么要返回这种迭代器呢?
} //特殊版本号[2] 重载形式
inline wchar_t *copy(const wchar_t *first, const wchar_t *last, wchar_t *result){
memmove(result, first, sizeof(wchar_t) * (last - first));
return result + (last - first);
} /*--------------------------------------------------------------------------------------
* __copy_dispatch 及其偏特化版本号
*/
//copy() 函数的泛化版本号中调用了一个 __copy_dispatch() 函数,此函数有一个泛化版本号和两个偏特化版本号
//全然泛化版本号
template<class InputIterator, class OutputIterator>
struct __copy_dispatch{
OutputIterator operator()(InputIterator first, InputIterator last, OutputIterator result){
return __copy(first, last, result, iterator_category(first));
}
}; //以下的两个偏特化版本号的參数为原生指针形式 //偏特化版本号[1],两个參数都是T *指针形式
template<class T>
struct __copy_dispatch<T *, T*>
{
T *operator()(T *first, T *last, T *result){
typedef typename __type_traits<T>::has_trivial_assignment_operator t;
return __copy_t(first, last, result, t());
}
}; //偏特化版本号[2]。第一个參数是 const T*指针形式,第二參数是 T *指针形式
template<class T>
struct __copy_dispatch<const T*, T*>
{
T *operator()(const T *first, const T *last, T *result){
typedef typename __type_traits<T>::has_trivial_assignment_operator t;
return __copy_t(first, last, result, t());
}
}; /*--------------------------------------------------------------------------------------
* 不同版本号的_copy 函数
*/
// 以下的 InputIterator 版本号、 RandomAccessIter 版本号 中的 InputIterator 和 RandomAccessIter 都仅仅是名称而已,编译器怎么知道详细化哪个函数版本号? ?
// --> 有个 input_iterator_tag 来确定迭代器的类型,从而使编译器能够知道该详细化哪个函数的版本号
//InputIterator 版本号
template<class InputIterator, class OutputIterator>
inline OutputIterator __copy(InputIterator first, InputIterator last, OutputIterator result, input_iterator_tag){
//假设仅仅是 InputIterator 的话。以迭代器赞同与否,决定循环是否继续、速度慢
for( ; first != last; ++result, ++first)
*result = *first;
return result;
}
// RandomAccessIter 版本号
template<class RandomAccessIter, class OutputIterator>
inline OutputIterator __copy(RandomAccessIter first, RandomAccessIter last, OutputIterator result, random_access_iterator_tag){
// 其它地方可能也会用到 __copy_d
return __copy_d(first, last, result, distance_type(first));
} template<class RandomAccessIter, class OutputIterator, class Distance>
inline OutputIterator __copy_d(RandomAccessIter first, RandomAccessIter last, OutputIterator result, Distance *){
// 以 n 决定循环的执行次数。速度快
for(Distance n = last - first; n > 0; --n, ++result, ++first)
*result = *first;
return result;
} /*--------------------------------------------------------------------------------------
* 偏特化版本号使用的 copy_t 函数
*/
//以下版本号适用于"指针所指之对象具备 trivial assignment operator"
template<class T>
inline T *__copy_t(const T* first, const T *last, T *result, __true_type){
memmove(result, first, sizeof(T) * (last - first));
return result + (last - first);
} //以下版本号适用于"指针所指之对象具备 non-trivial assignment operator"
template<class T>
inline T *__copy_t(const T* first, const T *last, T *result, __false_type){
return __copy_d(first, last, result, (ptrdiff_t *) 0); // 转而调用 __copy_d
}

STL 源代码剖析 算法 stl_numeric.h -- copy的更多相关文章

  1. STL 源代码剖析 算法 stl_algo.h -- partial_sort / partial_sort_copy

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie partial_sort / partial_sort_copy ------------- ...

  2. STL 源代码剖析 算法 stl_algo.h -- search

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie search --------------------------------------- ...

  3. STL 源代码剖析 算法 stl_algo.h -- random_shuffle

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie random_shuffle ------------------------------- ...

  4. STL 源代码剖析 算法 stl_algo.h -- partition

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie partition ------------------------------------ ...

  5. STL 源代码剖析 算法 stl_algo.h -- inplace_merge

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie inplace_merge(应用于有序区间) ----------------------- ...

  6. STL 源代码剖析 算法 stl_algo.h -- next_permutation

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie next_permutation ----------------------------- ...

  7. STL 源代码剖析 算法 stl_algo.h -- nth_element

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie nth_element ---------------------------------- ...

  8. STL 源代码剖析 算法 stl_algo.h -- lower_bound

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie lower_bound(应用于有序区间) ------------------------- ...

  9. STL 源代码剖析 算法 stl_algo.h -- merge sort

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie merge sort ----------------------------------- ...

随机推荐

  1. 重载new delete操作符是怎么调用的

    自定义的new操作符是怎么对英语new 一个对象的?自定义的delete操作符什么情况下得到调用?new一个对象时出现异常需要我操心内存泄露吗?下面的一个例子帮我们解开所有的疑惑. 1. 调用规则   ...

  2. Android高手进阶——Adapter深入理解与优化

    Android高手进阶--Adapter深入理解与优化 通常是针对包括多个元素的View,如ListView,GridView.ExpandableListview,的时候我们是给其设置一个Adapt ...

  3. 与众不同 windows phone (1) - Hello Windows Phone

    原文:与众不同 windows phone (1) - Hello Windows Phone [索引页] [源码下载] 与众不同 windows phone (1) - Hello Windows ...

  4. java.text.MessageFormat格式化字符串时的小技巧

    java.text.MessageFormat格式化字符串时的小技巧 public static void main(String[] args) throws InterruptedExceptio ...

  5. Linux入门基础#2:Linux文件系统基本结构

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  6. Ogre嵌入MFC傻瓜全然教程(三)

    经过前两两篇博文的解说.我们已经完毕了渲染工作,但仅仅是渲染而没有交互性,本篇博文我们就来加上事件的处理方法. 首先我们须要为项目加入一个帧监听类:CMyFrameListener,为了直观,在这直接 ...

  7. Abot 爬虫

    Abot 爬虫分析-整体结构 1. 引言 在Github 上搜索下Web Crawler 有上千个开源的项目,但是C#的仅仅只有168 个,相比于Java 或者Python 确实少的可怜.如果按照St ...

  8. 与众不同 windows phone (19) - Device(设备)之陀螺仪传感器, Motion API

    原文:与众不同 windows phone (19) - Device(设备)之陀螺仪传感器, Motion API [索引页][源码下载] 与众不同 windows phone (19) - Dev ...

  9. POJ2421 & HDU1102 Constructing Roads(最小生成树)

    嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree?   orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告 ...

  10. 关于java中的事件类型

    java中的Date是为了证明:天才的程序员也会犯错: java中的Calendar是为了证明:普通的程序员也会犯错. ———————————————————— stackoverflow上大部分都推 ...