极客班

来源:互联网 发布:手机淘宝如何取消代付 编辑:程序博客网 时间:2024/05/20 11:19

1.OOP(Obejct-Oriented programming)vs GP(Generic Programming)

OOP企图将datas和methods关联在一起
    template<class T, class Alloc=alloc>    class list{        ...        void sort();    };    template<class RandomAccessIterator>    inline void sort(RandomAccessIterator first, RandomAccessIterator last) {        if(first!=last) {            _introsort_loop(first, last, value_type(first), _lg(last-first)*2);            _final_insertion_sort(first, last);        }    }    template<class RandomAccessIterator, class T, class Size>    void _introsort_loop(RandomAccessIterator first,                         RandomAccessIterator last, T*, Size depth_limit) {        ...        RandomAccessIterator cut=_unguarded_partitio            (first, last, T(_median(*first, *(first+(last-first)/2, *(last-1))));        ...    }
GP将datas和methods分开来
        Data Structures(Containers)    template<class T, class Alloc=alloc>    class vector {        ...    };    template<class T, class Alloc=alloc, size_t BufSiz=0>    class deque {        ...    };    Algorithms    template<typename_RandomAccessIterator>    inline void    sort(_RandomAccessIterator _first, _RandomAccessIterator _last) {        ...    }    template<typename _RandomAccessIterator, typename _Compare>    inline void    sort(_RandomAccessIterator _first, _RandomAccessIterator _last, _Compare _comp) {        ...    }

2.分配器allocators

VC6的标准库,其allocator实现
    template<class _Ty>    class allocator {    public:        typedef _SIZT size_type;        typedef _PDFT difference_type;        typedef _Ty _FARQ *pointer;        typedef _Ty value_type;        pointer allocate(size_type _N, const void*) {            return (_Allocate((difference_type)_N, (pointer)0));        }        void deallocate(void _FARQ *_P, size_type) {            operator delete(_P);        }    };    template<class _Ty> inline    _Ty _FARQ *_Allocate(_PDET _N, _Ty _FARQ *) {        if(_N < 0) _N=0;        return ((_Ty _FARQ *)operator new((_SIZT)_N * sizeof(_Ty)));    }
BC5的标准库,其allocator实现如下
    template<class T>    class allocator    {    public:        typedef size_t size_type;        typedef ptrdiff_t difference_type;        typedef T* pointer;        typedef T value_type;        pointer allocate(size_type n, allocator<void>::const_pointer = 0) {            pointer tmp =             _RWSTD_STATIC_CAST(pointer, (::operator new                (_RWSTD_STATIC_CAST(size_t,(n * sizeof(value_type))))));            _RWSTD_THROW_NO_MSG(tmp == 0, bad_alloc);            return tmp;        }        void deallocate(pointer p, size_type) {            ::operator delete(p);        }        ...    };
G2.9标准库,其allocator实现
    template<class T>    class allocator {    public:        typedef T value_type;        typedef T* pointer;        typedef size_t size_type;        typedef ptrdiff_t difference_type;        poiter allocate(size_type n) {            return ::allocate((difference_type)n, (pointer)0);        }        void deallocate(pointer p) { ::dellocate(p) }    };    template<class T>    inline T* allocate(ptrdiff_t size, T*) {        set_new_handler(0);        T* tmp = (T*)                (::operator new(size_t)(size*sizeof(T))));        if(tmp == 0) {            cerr << "out of memory" << endl;            exit(1);        }        return tmp;    }    template<class T>    inline void deallocate(T* buffer) {        ::operator delete(buffer);    }
G4.9标准库,其allocator实现
    template<typename _Tp>    class allocator: public __allocator_base<_Tp> {        ...    };    template<typename _Tp>    class new_allocator {        ...        pointer        allocate(size_type __n, const void* = 0)        {             if (__n > this->max_size())            std::__throw_bad_alloc();            return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));        }        void        deallocate(pointer __p, size_type)        { ::operator delete(__p); }    };
0 0