一、vector定义摘要:
template <class T, class Alloc = alloc>
class vector {
public:
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type* iterator;
typedef const value_type* const_iterator;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
protected:
typedef simple_alloc<value_type, Alloc> data_allocator;
iterator start;
iterator finish;
iterator end_of_storage;
public:
iterator begin() { return start; }
const_iterator begin() const { return start; }
iterator end() { return finish; }
const_iterator end() const { return finish; }
size_type size() const { return size_type(end() - begin()); }
size_type max_size() const { return size_type(-1) / sizeof(T); }
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); }
const_reference operator[](size_type n) const { 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); }
explicit vector(size_type n) { fill_initialize(n, T()); }
};
二、构造函数 :
vector() : start(0), finish(0), end_of_storage(0) {}
vector(size_type n, const T& value) { fill_initialize(n, value); }
template <class T, class Alloc>
void vector<T, Alloc>::::fill_initialize(size_type n, const T& value)
{
start = allocate_and_fill(n, value);
finish = start + n;
end_of_storage = finish;
}
template <class T, class Alloc>
iterator vector<T, Alloc>::allocate_and_fill(size_type n, const T& x)
{
iterator result = data_allocator::allocate(n);
uninitialized_fill_n(result, n, x);
return result;
}
vector(const vector<T, Alloc>& x)
{
start = allocate_and_copy(x.end() - x.begin(), x.begin(), x.end());
finish = start + (x.end() - x.begin());
end_of_storage = finish;
}
iterator allocate_and_copy(size_type n, const_iterator first, const_iterator last)
{
iterator result = data_allocator::allocate(n);
uninitialized_copy(first, last, result);
return result;
}
三、析构函数:
template <class T, class Alloc>
vector<T, Alloc>::~vector()
{
destroy(start, finish);
deallocate();
}
template <class T, class Alloc>
void vector<T, Alloc>::deallocate()
{
if (start)
data_allocator::deallocate(start, end_of_storage - start);
}
四、push_back
template <class T, class Alloc>
void vector<T>::push_back(const T& x) //重点函数
{
if (finish != end_of_storage) //还有备用空间
{
construct(finish, x); //全局函数
++finish;
}
else
insert_aux(end(), x); //已经没有备用空间
}
template <class T, class Alloc>
void vector<T>::insert_aux(iterator position, const T& x)
{
if (finish != end_of_storage) //还有备用空间
{
construct(finish, *(finish - 1));
++finish;
T x_copy = x;
std::copy_backward(position, finish - 2, finish - 1);
*position = x_copy;
}
else //没有备用空间了
{
const size_type old_size = size();
const size_type len = old_size != 0 ? 2 * old_size : 1;
//以上配置原则:如果原大小为0,则配置1
//如果原大小不为0,则配置原大小2倍
//前半段用来放置原数据,后半段放置新数据
iterator new_start = data_allocator::allocate(len);
iterator new_finish = new_start;
new_finish = uninitialized_copy(start, position, new_start);
construct(new_finish, x);
++new_finish;
new_finish = uninitialized_copy(position, finish, new_finish);
destroy(begin(), end());
deallocate();
start = new_start;
finish = new_finish;
end_of_storage = new_start + len;
}
}
vetor的元素操作:pop_back、erase、clear、insert
void pop_back()
{
--finish; //将尾标记往前移动一格,表示将放弃尾端元素
destroy(finish); //析构函数
}
iterator erase(iterator first, iterator last)
{
iterator i = copy(last, finish, first);
destroy(i, finish);
finish = finish - (last - first);
return first;
}
iterator erase(iterator position)
{
if (position + 1 != end())
copy(position + 1, finish, position);
--finish;
destroy(finish);
return position;
}
void clear() { erase(begin(), end()); }
template <class T, class Alloc>
void vector<T, Alloc>::insert(iterator position, size_type n, const T & x)
{
if (n != 0) //当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 //"插入点之后的现有元素个数"小于等于"新增元素个数"
{
uninitialized_fill_n(finish, n - elems_after, x_copy);
finish += n - elems_after;
uninitialized_copy(position, old_finish, finish);
finish += elems_after;
fill(position, old_finish, x_copy);
}
}
else
{
//备用空间小于"新增元素个数"(那就必须配置额外的内存)
//首先决定新长度:旧长度的两倍,或旧长度+新增元素个数
const size_type old_size = size();
const size_type len = old_size + max(old_size, n);
iterator new_start = data_allocator::allocate(len);
iterator new_finish = new_start;
new_finish = uninitialized_copy(start, position, new_start);
new_finish = uninitialized_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;
}
}
}
五、resize、reverse
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 reserve(size_type n)
{
if (capacity() < n)
{
const size_type old_size = size();
iterator tmp = allocate_and_copy(n, start, finish);
destroy(start, finish);
deallocate();
start = tmp;
finish = tmp + old_size;
end_of_storage = start + n;
}
}
iterator allocate_and_copy(size_type n, const_iterator first, const_iterator last)
{
iterator result = data_allocator::allocate(n);
uninitialized_copy(first, last, result);
return result;
}
六、 swap
void swap(vector<T, Alloc>& x)
{
__STD::swap(start, x.start);
__STD::swap(finish, x.finish);
__STD::swap(end_of_storage, x.end_of_storage);
}