vector实现

给出的实现,参照accelerated c++一书当中的内容。

总结

有几个点说一下啊:
1.注意三个指针的语义,data,avail,limit。增加limit之后,注意,修改avail的用法,尤其是迭代器那一块。
2.rule of three原则。
3.allocator的用法,allocate,deallocate,construct,destroy的用法,以及为什么要自己管理。我们只是使用了内存管理工具而已。
4.模板定义与实现不能分离。
5.类外定义模板方法,注意类的作用域要写完整,包好模板类型。

代码

#ifndef VEC_H_
#define VEC_H_
#include <memory>
#include <utility>

namespace ACC {

template<class T> 
class vector{

public:
    typedef T* iterator;
    typedef const T* const_iterator;
    typedef T& reference;
    typedef const T& const_reference;
    typedef T value_type;
    typedef std::size_t size_type;

public:
    vector(){create();};
    explicit vector(int n, const T& val = T()){ create( n, val ); };
    vector( const vector& rhs ){ create( rhs.begin(), rhs.end() ); }
    vector& operator=( const vector& rhs );
    ~vector(){ uncreate(); }

public:
    size_type size() const { return avail - data; }

    T& operator[]( size_type i ) { return data[i]; }
    const T& operator[]( size_type i ) const  { return data[i]; }

    iterator begin()  { return data; }
    const_iterator begin() const { return data; }
    iterator end()  { return avail; }
    const_iterator end() const { return avail; }

    void push_back(const T& val) {
        if( avail == limit )
            grow();
        unchecked_append(val);
    }

private:
    T* data;
    T* avail;
    T* limit;
private:
    void create(){ data = avail = limit = 0; }
    void create( int n, T val);
    void create( const_iterator b, const_iterator e ); 
    void uncreate();
private:
    std::allocator<T> alloc;
    void grow();
    void unchecked_append(const T& val);
};// vector

template<class T>
vector<T>& vector<T>::operator=( const vector& rhs ) {
    if( this != &rhs ){
        uncreate();
        create(rhs.begin(), rhs.end());
    }
    return *this;
}

template<class T>
void vector<T>::create( int n, T val ) {
    data = alloc.allocate(n);
    avail = limit = data+n;
    std::uninitialized_fill( data, avail, val );
}

template<class T>
void vector<T>::create( const_iterator b, const_iterator e ){
    data = alloc.allocate(e - b);
    avail = limit = std::uninitialized_copy( b, e, data );
}

template<class T>
void vector<T>::uncreate() {
    if( data ){
        iterator it = avail;
        while( it != data ){
            alloc.destroy(--it);
        }

        alloc.deallocate(data, limit-data);
    }
    data = avail = limit = 0;
}

template<class T>
void vector<T>::grow() {
    size_type new_size = std::max( 2*(limit - data), std::ptrdiff_t(1) );
    iterator new_data = alloc.allocate(new_size);
    iterator new_avail = std::uninitialized_copy( data, avail, new_data );
    iterator new_limit = new_data + new_size;

    uncreate();

    data = new_data;
    avail = new_avail;
    limit = new_limit;
}

template<class T>
void vector<T>::unchecked_append( const T& val ) {
    alloc.construct(avail++, val);
}

}// namespace ACC

#endif // VEC_H_
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值