STL源码剖析 数值算法 copy 算法

31 篇文章 2 订阅
  • copy复制操作,其操作通过使用assignment operator 。针对使用trivial assignment operator的元素型别可以直接使用内存直接复制行为(使用C函数 memove或者memcpy)节约时间。
  • 还可以通过函数重载(function overloading)、型别特性(type traits)、偏特化(partial specialization)等技巧进一步强化效率。

  • copy函数将[first,last) 内的元素复制到区间[result,result+(last-first))内
  • 返回迭代器指向的是  result+(last-first)
  • 输入区间使用 InputIterator,输出区间使用 OutputIterator
  • 注意事项:1,result位于[first , last)之内的时候也就是输出区间的起头与输入区间重叠,不能使用copy;如果输出区间的尾端和输入区间重叠,就可以使用

  • 上述第二种情况可能出现错误,是可能。如果输出区间的起点位于输入区间内部,copy算法可能会在输入区间的(某些)元素尚未复制之前就将其覆盖其值,导致错误
  • 如果copy算法根据其所接收的迭代器特性 决定使用memmove()来执行任务,就不会出现问题,因为memmove 会将整个输入区间的内容复制下来就不会出现覆盖的危险
#include <iostream>   //std::cout
#include <algorithm>  //std::fill
#include <vector>     //std::vector
#include <deque>

template <class T>
struct display{
    void operator()(const T& value){
        std::cout << value <<' ';
    }
};



int main(){
    {
        int ia[] = {0,1,2,3,4,5,6,7,8};
        //输出区间的终点和输入区间出现重叠 没有问题
        std::copy(ia+2,ia+7,ia);
        std::for_each(ia,ia+9,display<int>()); //2 3 4 5 6 5 6 7 8
        std::cout << std::endl;
    }
    {
        int ia[] = {0,1,2,3,4,5,6,7,8};
        //输出区间的起点和输入区间出现重叠 可能会出现问题
        std::copy(ia+2,ia+7,ia+4);
        std::for_each(ia,ia+9,display<int>()); //0 1 2 3 2 3 4 5 6
        std::cout << std::endl;
        //本例结果正确 因为调用的copy算法使用memmove()执行实际复制操作
    }
    {
        int ia[] = {0,1,2,3,4,5,6,7,8};
        std::deque<int>id(ia,ia+9);
        std::deque<int>::iterator first = id.begin();
        std::deque<int>::iterator last = id.end();
        ++++first;  //advance(first,2)  2
        std::cout << * first << std::endl;
        ----last;  //advance(last,-2) 7
        std::cout << * last << std::endl;

        std::deque<int>::iterator result = id.begin();
        std::cout << *result << std::endl; //0
        //输出区间的终点和输入区间重叠 没有问题
        std::copy(first,last,result);
        std::for_each(id.begin(),id.end(),display<int>()); //2 3 4 5 6 5 6 7 8
        std::cout << std::endl;
    }
    {
        int ia[] = {0,1,2,3,4,5,6,7,8};
        std::deque<int>id(ia,ia+9);
        std::deque<int>::iterator first = id.begin();
        std::deque<int>::iterator last = id.end();
        ++++first;  //advance(first,2)  2
        std::cout << * first << std::endl;
        ----last;  //advance(last,-2)
        std::cout << * last << std::endl;

        std::deque<int>::iterator result = id.begin();
        std::advance(result,4);
        std::cout << *result << std::endl; //4
        //输出区间的起点和输入区间重叠 可能会出现问题
        std::copy(first,last,result);
        std::for_each(id.begin(),id.end(),display<int>()); //0 1 2 3 2 3 4 5 6
        //STL源码剖析 此处会出现错误,原因在于copy算法不再使用memcove()执行实际的复制操作
        //但是实际没有出现错误
        std::cout << std::endl;
        /*
        2 3 4 5 6 5 6 7 8 
        0 1 2 3 2 3 4 5 6 
        2
        7
        0
        2 3 4 5 6 5 6 7 8 
        2
        7
        4
        0 1 2 3 2 3 4 5 6 
         */
    }


}
  • 使用vector代替上述的deque进行测试复制结果是正确的,因为vector的迭代器其实就是个原生指针
  • copy更改的是迭代器所指向的对象,并不是修改迭代器本身,他会为区间内的元素赋予新的数值,但不是产生新的元素,也不会改变迭代器的个数。即,copy不能用来将元素插入到空的容器中
  • 使用copy算法配合序列容器的insert_iterator 

 

#include <iostream>   //std::cout

//完全泛化的版本
template <class InputIterator,class OutputIterator>
inline OutputIterator copy(InputIterator first,InputIterator last,
                           OutputIterator result){
    return __copy_dispatch<InputIterator ,OutputIterator>()(first,last,result);
}

//两个重载函数 针对原生指针(视为一种特殊的迭代器)const char* 和 const wchar_t 进行内存的直接拷贝操作
//特殊版本1 重载形式 const char *
inline char* copy(const char* first,const char* last,char* result){
    std::memmove(result,first,last - first);
    return result + (last - first);
}

//特殊版本2 重载形式 const wchar_t *
inline wchar_t* copy(const wchar_t* first,const wchar_t* last,wchar_t* result){
    std::memmove(result,first,sizeof(wchar_t)*(last - first));
    return result + (last - first);
}

//copy泛化版本调用了一个 __copy_dispatch函数,这个函数有一个完全泛化版本和两个偏特化版本
//完全泛化版本
//__copy_dispatch完全泛化版本根据迭代器的种类不同,调用了不同的__copy(),是因为不同种类的迭代器使用的循环条件不同,有快有慢
template <class InputIterator,class OutputIterator>
struct __copy_dispatch{
    OutputIterator operator()(InputIterator first,InputIterator last,
            OutputIterator result){
        return __copy(first,last,result,iterator_category(first));
    }
};

//InputIterator 版本
template <class InputIterator,class OutputIterator>
inline OutputIterator __copy(InputIterator first,InputIterator last,
                             OutputIterator result,std::input_iterator_tag){
    //通过迭代器的等同与否 决定循环是否继续  速度很慢
    while(first!=last){
        *result = *first; //assignment operator
        ++first;
        ++result;
    }
    return result;
}

//RandomAccessIterator 版本
template <class RandomAccessIterator,class OutputIterator>
inline OutputIterator __copy(RandomAccessIterator first,RandomAccessIterator last,
                             OutputIterator result,std::random_access_iterator_tag){
    //划分新的函数 为了让其余地方可以使用
    return __copy_d(first,last,result, distance_type(first));
}

template <class RandomAccessIterator,class OutputIterator,class Distance>
inline OutputIterator __copy_d(RandomAccessIterator first,RandomAccessIterator last,
                               OutputIterator result,Distance*){
    //使用n决定循环的执行次数 速度快
    Distance n = last - first;
    while (n>0){
        *result = *first; //assignment operator
        ++result;
        ++first;
        n--;
    }
    return result;
}



//偏特化版本1 两个参数的型别都是T* 指针
template<class T>
struct __copy_dispatch<T*,T*>
{
    T* operator()(T* first,T* last,T* result){
        typedef typename __type_traits<T>::has_trival_assignment_operator t;
        return __copy_t(first,last,result,t());
    }
};

//偏特化版本2 第一个参数是constT*指针形式 第二个参数是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_trival_assignment_operator t;
        return __copy_t(first,last,result,t());
    }
};

//在上述偏特化版本指定参数类型为T*和const T*只针的前提下进一步探测 
//探测指针所指之物是否具备trivial assignment operator (平凡赋值操作符)
//SGI STL通过使用__type_traits<>编程技巧 和 增加一层间接性 来区分不同的 __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){
    //原生指针是一种 RandomAccessIterator 所以交给 __copy_d()完成
    return __copy_d(first,last,result,(std::ptrdiff_t)0);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值