使用必须包含头文件<algorithm>
由于copy进行的是复制操作,而复制操作不外乎assignment operator和copy constructor(copy算法用的是前者),但是某些元素型别用于的是trivial assginment operator,因此,如果能够使用内存直接复制行为(例如C标准函数memmove或memcpy),变能够节省大量时间。为此,SGI STL的copy算法用尽各种办法,包括函数重载、性别特性、偏特化等编程技巧,无所不用其极地加强效率。如下图所示:
copy算法是一一进行元素的复制操作,如果输出区间的起点位于输入区间内,copy算法便(可能)会在输入区间的(某些)元素尚未被复制之前,就覆盖其值,导致错误结果。我们使用”可能”这个字眼,是因为如果copy算法根据其所接收的迭代器的特性决定调用memmove()来执行任务,就不会造成上述错误,因为memmove()会先将整个输入区间的内容复制下来,没有被覆盖的危险。
以下是copy的实现代码:
// copy 函数运用function overloading, type traits,partial // specialization, 无所不用其极的改善效率 //copy有一个全特化和两个偏特化版本 template<class InputIterator, class OutputIterator> inlineOutputIterator copy(InputIterator first, InputIterator last, OutputIteratorresult) { return__copy_dispatch<InputIterator,OutputIterator>()(first, last, result); } inline char* copy(const char* first, const char* last, char*result) { memmove(result, first, last - first); return result + (last - first); } inline wchar_t* copy(const wchar_t* first, constwchar_t* last, wchar_t*result) { memmove(result, first, sizeof(wchar_t) * (last - first)); return result + (last - first); } //__copy_dispatch有一个完全泛华版本和两个偏特化版本 template<class InputIterator, class OutputIterator> struct__copy_dispatch { OutputIterator operator()(InputIteratorfirst, InputIterator last, OutputIterator result) { return __copy(first, last, result,iterator_category(first)); } }; 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()); } }; template<class T> struct__copy_dispatch<const T*, T*> { T* operator()(constT* 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迭代器设计, //另一个对输入参数类型为RandomAccessIterator迭代器设计 template<class InputIterator, class OutputIterator> inlineOutputIterator __copy(InputIterator first, InputIterator last, OutputIteratorresult, input_iterator_tag) { for ( ; first != last; ++result, ++first) *result = *first; return result; } template<class RandomAccessIterator, class OutputIterator> inlineOutputIterator __copy(RandomAccessIterator first,RandomAccessIterator last, OutputIterator result, random_access_iterator_tag) { return __copy_d(first, last, result,distance_type(first)); } //__copy_d()是对输入参数为RandomAccessIterator的实现 template<class T> struct__copy_dispatch<const T*, T*> { T* operator()(constT* first, const T* last, T* result) { typedef typename__type_traits<T>::has_trivial_assignment_operator t; return __copy_t(first, last, result,t()); } }; //__copy_t()有两个版本,分别对应指针所指向对象具备trival assignmentoperator //和non-trival assignment operator template<class T> inlineT* __copy_t(const T* first, const T* last, T* result, __true_type) { memmove(result, first, sizeof(T) * (last- first)); return result + (last - first); } template<class T> inlineT* __copy_t(const T* first, const T* last, T* result, __false_type) { return __copy_d(first, last, result, (ptrdiff_t*) 0); }
copy_backward()以逆行方向复制,实现技巧与copy相似。
原文:http://blog.csdn.net/walkerkalr/article/details/23739755