vector源码剖析
生活随笔
收集整理的這篇文章主要介紹了
vector源码剖析
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、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()); } };?
二、構(gòu)造函數(shù) :
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; }?
三、析構(gòu)函數(shù):
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) //重點(diǎn)函數(shù) {if (finish != end_of_storage) //還有備用空間{construct(finish, x); //全局函數(shù)++finish;}elseinsert_aux(end(), x); //已經(jīng)沒有備用空間 }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 //沒有備用空間了{(lán)const size_type old_size = size();const size_type len = old_size != 0 ? 2 * old_size : 1;//以上配置原則:如果原大小為0,則配置1//如果原大小不為0,則配置原大小2倍//前半段用來放置原數(shù)據(jù),后半段放置新數(shù)據(jù)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; //將尾標(biāo)記往前移動一格,表示將放棄尾端元素destroy(finish); //析構(gòu)函數(shù) }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) //當(dāng)n!= 0才進(jìn)行以下所有操作{if (size_type(end_of_storage - finish) >= n) //備用空間大于等于新增元素{T x_copy = x;const size_type elems_after = finish - position; //計(jì)算插入點(diǎn)之后的現(xiàn)有元素個(gè)數(shù)iterator old_finish = finish;if (elems_after > n) //"插入點(diǎn)之后的現(xiàn)有元素個(gè)數(shù)"大于"新增元素個(gè)數(shù)"{uninitialized_copy(finish - n, finish, finish);finish += n;copy_backward(position, old_finish - n, old_finish);fill(position, position + n, x_copy);}else //"插入點(diǎn)之后的現(xiàn)有元素個(gè)數(shù)"小于等于"新增元素個(gè)數(shù)"{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{//備用空間小于"新增元素個(gè)數(shù)"(那就必須配置額外的內(nèi)存)//首先決定新長度:舊長度的兩倍,或舊長度+新增元素個(gè)數(shù)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());elseinsert(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);}?
總結(jié)
以上是生活随笔為你收集整理的vector源码剖析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: select、poll、epoll优缺点
- 下一篇: 成都欢乐谷夏天夜场几点到几点