码迷,mamicode.com
首页 > 其他好文 > 详细

STL之vector

时间:2015-03-15 16:46:32      阅读:131      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!