首页 > 其他 > 详细

STL之vector

时间:2015-03-15 16:46:12      阅读:250      评论:0      收藏:0      [点我收藏+]

vector的实现技术,关键在于其对大小的控制以及重新配置时的数据移动效率

template<class T, class Alloc = alloc>
class vector
{
public:
    typedef T* iterator;
protected:
    iterator start;
    iterator finish;
    iterator end_of_storate;
    void fill_initialize(size_type n, const T& value)
    {
        start = allocate_and_fill(n, value);
        finish = start + n;
        end_of_storage = finish;
    }
public:
    vector(): start(0), end(0), end_of_storage(0){}
    vector(int n, const T& value){ fill_initialize(n, value); } 
    explicit vector(size_type n){ fill_initialize(n, T()); }
vector(const vector<T, Alloc>& x)
{
start = allocate_and_copy(x.end()-x.begin(), x.begin(), x.end());
finish = start + (x.end()-x.start());
end_of_storage = finish;
}

void reverse(size_type n)
{
if(capacity() < n)
{
const size_type old_size = size();
itertor tmp = allocate_and_copy(n, start, finish);
destroy(start, finish);
deallocate();
start = tmp;
finish = start + old_size;
end_of_storage = start + n;
}
}

void push_back(const T& x)
{
if(finish != end_of_storage)
{
construct(finish, x);
++finish;
}
else
insert_aux(end(), x);
} };

 

STL之vector

原文:http://www.cnblogs.com/zhoudan/p/4339940.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!