vector 实现

vector的实现。
内存申请和释放用了simple_alloc。

#include<new>
#include<stdlib.h>
using namespace std;
template <class T1, class T2>
inline void construct(T1* p, const T2& value)
{
    new (p)T1(value);
}
size_t max(size_t a, size_t b)
{
    if (a > b)
        return a;
    else
        return b;
}

template <class F , class Size , class T>
F unintialized_fill_n(F first, Size n,const T& x)//没有完成is_POD不能对class和struct型变量进行处理
{
    F cur = first;
    for (; n > 0; --n, ++cur)
    {
        construct(&*cur, x);
    }
    return cur;
}
template <class I , class O>
inline O copy(I first, I last , O result)
{
    for (; first != last; ++result, ++first)
        *result = *first;
    return result;
}
template <class I ,class F >
F uninitialized_copy(I first, I last, F result)
{
    F cur = result;
    for (; first != last; ++first, ++cur)
    {
        construct(&*cur, *first);
    }
    return cur;
}


template <class T>
inline void destroy(T * pointer)
{
    pointer->~T();//显式的调用T的析构
}
template <class T>
inline void destroy(T * first, T * last)
{
    for (; first < last; ++first)
    {
        destroy(&*first);
    }
}
template <class T>
class simple_alloc
{
private:
    static void * allocate_malloc(size_t n)
    {
        void * result = malloc(n);
        return result;
    }
    static void deallocate(void * p, size_t)
    {
        free(p);
    }
public:
    static T * allocate(size_t n)
    {
        if (n == 0)
            return 0;
        else
        {
            return (T*)allocate_malloc(n * sizeof(T));
        }
    }
    static void deallocate(T *p, size_t n)
    {
        if (0 != n)
        {
            dellocate_free(p, n*sizeof(T));
        }

    }
    static void deallocate(T *p)
    {
        dellocate_free(p, sizeof(T));
    }
};


template <class T>
class vector
{
public:
    typedef T           value_type;
    typedef value_type* pointer;
    typedef value_type* iterator;
    typedef value_type& reference;
    typedef size_t  size_type;
    typedef ptrdiff_t difference_type;

protected:

    typedef simple_alloc<value_type> data_allocate;

    pointer start_;
    pointer finish_;
    pointer end_of_storage_;

    void deallocate()
    {
     //   if (start_)
         //   data_allocate::dellocate(start_, (size_t)end_of_storage_ - start_);
    }

    void insert_auex(iterator position, const T& x)
    {
        const size_type old_size = size();
         size_type length;
        if (old_size == 0)
            length = 1;
        else
            length = 2 * old_size;

        pointer new_start = data_allocate::allocate(length);
        pointer new_finish = new_start;
        new_finish = uninitialized_copy(start_, position, new_start);
        construct(new_finish, x);
        ++new_finish;


        destroy(begin(), end());
        deallocate();

        start_ = new_start;
        finish_ = new_finish;
        end_of_storage_ = start_ + length;
    }




    void allocate_and_fill(size_type n, const T& x)
    {
        iterator result = data_allocate::allocate(n);
        unintialized_fill_n(result, n, x);
        return result;
    }
    void fill_initialize(size_type n, const T& value)
    {
        start_ = allocate_and_fill(n, value);
        finish_ = start_ + n;
        end_of_storage_ = finish_;
    }

public:
    pointer begin()
    {
        return start_;
    }
    pointer end()
    {
        return finish_;
    }

    size_type size()
    {
        return size_type(end() - begin());
    }

    size_type capacity () const 
    {
        return size_type(end_of_storage_ - begin());
    }
    bool empty () const
    {
        return begin() == end();
    }
    reference operator[](size_type n)
    {
        return *(begin() + n);
    }


    vector() : start_(0), finish_(0), end_of_storage_(0) {}
    vector(size_type n, const T& value)
    {
        fill_initialize(n, value);
    }
    vector(int n, const T& value)
    {
        fill_initialize(n, value);
    }
    vector(long n, const T& value)
    {
        fill_initialize(n, value);
    }


    ~vector()
    {
        destroy(start_, finish_);//先析构数据成员
        deallocate();///再释放空间
    }
    reference front()
    {
        return *begin();
    }
    reference back()
    {
        return *end();
    }
    void push_back(const T& x)
    {
        if (finish_ != end_of_storage_)
        {
            construct(finish_, x);
            ++finish_;
        }
        else
            insert_auex(end(), x);
    }

    void pop_back()
    {
        --finish_;
        destory(finish_);
    }

    pointer erase(iterator position)
    {
        if (position + 1 != end())
        {
            copy(position + 1, finish_, position);
        }
        destory(finish_);
        return position;
    }

    pointer erase(pointer first, pointer last)
    {
        pointer i = copy(last, finish_, first);
        destory(i, finish_);
        finish_ = finish_ - (last - first);
        return first;
    }

    void insert(pointer position, size_type n, const T& x)
    {
        if (n != 0)
        {
            if (size_type(end_of_storage_ - finish_) >= n)
            {
                T x_copy = x;
                const size_type elems_after = finish_ - position;
                iterator old_finish = finish_;
                if (elems_after > n)
                {
                    uninitialized_copy(finish_-n, finish_, finish_);
                    finish_ += n;
                    copy_backward(position, old_finish - n, old_finish);
                    fill(position, position + n, x_copy);

                }
                else
                {
                    const size_type old_size = size();
                    const size_type len = old_size + max(old_size, n);
                    pointer new_start = data_allocate::allocate(len);
                    pointer new_finish = new_start;
                    new_finish = uninitialized_copy(start_, position, new_start);
                    new_finish = unintialized_fill_n(new_finish, n, x);
                    new_finish = uninitialized_copy(position, finish_, new_finish);

                destroy(start_, finish_);
                deallocate();
                start_ = new_start;
                finish_ = new_finish;
                end_of_storage_ = new_start + len;
                }
            }
        }
    }

    void resize(size_type new_size, const T& x)
    {
        if (new_size < size())
        {
            erase(begin() + new_size, end());
        }
        else
        {
            insert(end(), new_size - size(), x);
        }
    }
    void resize(size_type new_size)
    {
        resize(new_size, T());
    }
    void clear()
    {
        erase(begin(), end());
    }


};

参考书籍《STL源码剖析》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值