标签:
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);
} };
标签:
原文地址:http://www.cnblogs.com/zhoudan/p/4339940.html