SGI STL 学习笔记二 vector
sequence containers
- Array
- Vector
- Heap
- Priority_queue
- Heap
- List
- sList(not in standard)
- Deque
- Stack
- Queue
Sequence Containers 其中的元素 都是可序的(ordered),但并不一定有序(sorted)。STL 中有vector ,list ,deque,stack,queue,priority_queue等序列容器。Stack queue 由于只是將deque重新封裝而成,在技術(shù)上被歸類為一種配接器(adapter)。
Vector
- Vector 的數(shù)據(jù)為動(dòng)態(tài)空間,隨著元素的加入。內(nèi)部會(huì)通過機(jī)制自行擴(kuò)充空間,以容納新元素。
- Vector 的效率,在于對(duì)大小的控制,重新分配時(shí)數(shù)據(jù)移動(dòng)效率。當(dāng)空間不足時(shí),vector會(huì)選擇策略擴(kuò)充容量。
- Vector resize之后,很可能使所有迭代器均失效。 插入后,插入點(diǎn)之前的Iterator 有效,其他則無效。eraser迭代器失效。
Vector實(shí)現(xiàn)
Vector 實(shí)現(xiàn)比較簡單。這里僅僅作為打開SGI STL的敲門磚。
我這里的SGI STL 對(duì)vector有進(jìn)行了進(jìn)一步封裝。在頭文件中,也給出了我們的解釋。
// The vector base class serves two purposes. First, its constructor
// and destructor allocate (but don't initialize) storage. This makes
// exception safety easier. Second, the base class encapsulates all of
// the differences between SGI-style allocators and standard-conforming
// allocators.
這里根據(jù) 宏 __STL_USE_STD_ALLOCATORS 來決定是否資源分配器。如果定義了__STL_USE_STD_ALLOCATORS, 則使用allocator< _Tp >,否則為alloc
//這里的 _Vector_base 為我們隱藏了 使用STL 標(biāo)準(zhǔn)分配器,和SGI 自己特有的分配器之間的不同 //我們現(xiàn)在先把這里具體的分配細(xì)節(jié)透明。 //這是,使用SGI 自己的分配器 template <class _Tp, class _Alloc> class _Vector_base { public:typedef _Alloc allocator_type;allocator_type get_allocator() const { return allocator_type(); }_Vector_base(const _Alloc&): _M_start(0), _M_finish(0), _M_end_of_storage(0) {}_Vector_base(size_t __n, const _Alloc&): _M_start(0), _M_finish(0), _M_end_of_storage(0) {_M_start = _M_allocate(__n);_M_finish = _M_start;_M_end_of_storage = _M_start + __n;}~_Vector_base() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); }protected:_Tp* _M_start;_Tp* _M_finish;_Tp* _M_end_of_storage;typedef simple_alloc<_Tp, _Alloc> _M_data_allocator;_Tp* _M_allocate(size_t __n){ return _M_data_allocator::allocate(__n); }void _M_deallocate(_Tp* __p, size_t __n) { _M_data_allocator::deallocate(__p, __n); } };?
template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) > class vector : protected _Vector_base<_Tp, _Alloc> { private:typedef _Vector_base<_Tp, _Alloc> _Base; public:typedef _Tp 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;typedef typename _Base::allocator_type allocator_type;allocator_type get_allocator() const { return _Base::get_allocator(); } … ... };?
分析vector,首先看他的Iterator。
typedef value_type* iterator;
typedef const value_type* const_iterator;
我們可以看出,vector的Iterator 就是一個(gè)指針。若是定義
vector<int>:: iterator iter1;
vector<RECT>:: iterator iter2;
那么,Iter1 其實(shí),就是int *, iter2其實(shí)就是RECT * 。
看一下,部分的vector 函數(shù),也是我們常常使用的。
Vector 的數(shù)據(jù),什么時(shí)候被釋放。我們需要看析構(gòu)函數(shù)。
~vector() { destroy(_M_start, _M_finish); }template <class _ForwardIterator>inline void destroy(_ForwardIterator __first, _ForwardIterator __last) {__destroy(__first, __last, __VALUE_TYPE(__first));}#define __VALUE_TYPE(__i) __value_type(__i)下面是2個(gè)偏特化版本??梢钥闯?#xff0c;在一些特殊情況下,我們找到了最快速的方法。什么也不干。
inline void destroy(char*, char*) {}inline void destroy(wchar_t*, wchar_t*) {}template <class _Iter>inline typename iterator_traits<_Iter>::value_type*__value_type(const _Iter&){//這里,僅僅構(gòu)造一個(gè)臨時(shí)對(duì)象(準(zhǔn)確說是指針)來做返回值,事實(shí)上,我們不關(guān)心他到底是個(gè)什么,只是關(guān)心她的類型。//用這個(gè)類型來激發(fā)函數(shù)重載,所以,用0來構(gòu)造也無妨。return static_cast<typename iterator_traits<_Iter>::value_type*>(0);}template <class _ForwardIterator, class _Tp>inline void__destroy(_ForwardIterator __first, _ForwardIterator __last, _Tp*)//這里多了一個(gè)接受這個(gè)類型對(duì)象參數(shù){//根據(jù)這個(gè)類型_Tp,我們根據(jù)__type_traits<_Tp>,找到了這個(gè)類型是否有has_trivial_destructor。typedef typename __type_traits<_Tp>::has_trivial_destructor _Trivial_destructor;//然后構(gòu)造一個(gè)臨時(shí)的對(duì)象來激發(fā)函數(shù)重載。__destroy_aux(__first, __last, _Trivial_destructor());}//下面2個(gè)便是特化后的結(jié)果。//__false_type,我們老老實(shí)實(shí)的該干什么干什么。template <class _ForwardIterator>inline void__destroy_aux(_ForwardIterator __first, _ForwardIterator __last, __false_type){for ( ; __first != __last; ++__first)destroy(&*__first);}//__true_type 我們實(shí)在是沒有這個(gè)必要和他糾結(jié)了。template <class _ForwardIterator>inline void __destroy_aux(_ForwardIterator, _ForwardIterator, __true_type) {}這是可能懷疑,內(nèi)存到那里釋放呢? 別忘了,我們的vector 是繼承自_Vector_base,內(nèi)存釋放,管理都隱藏在他那里。
~_Vector_base() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); }
這里才真正的執(zhí)行內(nèi)存的回收。但是這里又涉及到了SGI STL 的內(nèi)存管理,這部分是給操作系統(tǒng),還是給內(nèi)存池呢?
在沒有研究過細(xì)致的內(nèi)存管理之前。我們還是將這里透明吧。
基本操作
iterator begin() { return _M_start; }const_iterator begin() const { return _M_start; }iterator end() { return _M_finish; }const_iterator end() const { return _M_finish; }size_type size() const { return size_type(end() - begin()); }size_type capacity() const { return size_type(_M_end_of_storage - begin()); }bool empty() const { return begin() == end(); }void push_back(const _Tp& __x) {if (_M_finish != _M_end_of_storage) {construct(_M_finish, __x);++_M_finish;}else_M_insert_aux(end(), __x);}void push_back() {if (_M_finish != _M_end_of_storage) {construct(_M_finish);++_M_finish;}else_M_insert_aux(end());}void resize(size_type __new_size, const _Tp& __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, _Tp()); }?
刪除 erase
iterator erase(iterator __position) {if (__position + 1 != end())copy(__position + 1, _M_finish, __position);--_M_finish;destroy(_M_finish);return __position;}iterator erase(iterator __first, iterator __last) {iterator __i = copy(__last, _M_finish, __first);destroy(__i, _M_finish);_M_finish = _M_finish - (__last - __first);return __first;}Copy 是全局函數(shù),操作簡單,同樣有多個(gè)特化版本。Vector 和一般數(shù)組的刪除動(dòng)作一樣,將后面元素一個(gè)個(gè)往前搬。最后修改個(gè)數(shù)。
插入 insert
iterator insert(iterator __position, const _Tp& __x) {size_type __n = __position - begin();if (_M_finish != _M_end_of_storage && __position == end()) {construct(_M_finish, __x);++_M_finish;}else_M_insert_aux(__position, __x);return begin() + __n;}iterator insert(iterator __position) {size_type __n = __position - begin();if (_M_finish != _M_end_of_storage && __position == end()) {construct(_M_finish);++_M_finish;}else_M_insert_aux(__position);return begin() + __n;}template <class _Tp, class _Alloc>void vector<_Tp, _Alloc>::_M_insert_aux(iterator __position){if (_M_finish != _M_end_of_storage) {construct(_M_finish, *(_M_finish - 1));++_M_finish;copy_backward(__position, _M_finish - 2, _M_finish - 1);*__position = _Tp();}else {const size_type __old_size = size();const size_type __len = __old_size != 0 ? 2 * __old_size : 1;iterator __new_start = _M_allocate(__len);iterator __new_finish = __new_start;__STL_TRY {__new_finish = uninitialized_copy(_M_start, __position, __new_start);construct(__new_finish);++__new_finish;__new_finish = uninitialized_copy(__position, _M_finish, __new_finish);}__STL_UNWIND((destroy(__new_start,__new_finish), _M_deallocate(__new_start,__len)));destroy(begin(), end());_M_deallocate(_M_start, _M_end_of_storage - _M_start);_M_start = __new_start;_M_finish = __new_finish;_M_end_of_storage = __new_start + __len;}}的確很簡單,和我們?cè)趯W(xué)校學(xué)的并沒有什么大的不同,只是在對(duì)新增元素的構(gòu)造上不同。
construct(__new_finish),
construct(_M_finish, *(_M_finish - 1));
以上construct是全局函數(shù),同樣有特化版本。將類的構(gòu)造分成,資源分配 + 構(gòu)造函數(shù),來做到提高效率。這樣在大量數(shù)據(jù)上效果應(yīng)該很明顯,并沒有具體測(cè)試。
?
對(duì)一次插入大量元素時(shí),vector 的策略是。if (插入元素個(gè)數(shù) == 0 ) return if (判斷容量是否足夠){if (插入點(diǎn)后的元素個(gè)數(shù) > 待插入元素個(gè)數(shù)){按照最后一個(gè)元素,構(gòu)造插入元素個(gè)數(shù)個(gè)元素。向插入點(diǎn)數(shù)據(jù)向后搬運(yùn)。移動(dòng)指針。將待插入元素順次插入。}else{先以__x構(gòu)造元素,在不需要移動(dòng)位置的地方。將原來的元素,移動(dòng)到最后。在插入位置處,以__x構(gòu)造元素。}}else{根據(jù)策略分配空間(這里至少PJ 和SGI的策略不同,這里應(yīng)該和不同的內(nèi)存管理策略有關(guān))將插入點(diǎn)之前的原有的數(shù)據(jù)復(fù)制到新空間依次復(fù)制新元素到新空間。依次復(fù)制原來數(shù)據(jù)到新空間} template <class _Tp, class _Alloc>void vector<_Tp, _Alloc>::insert(iterator __position, size_type __n, const _Tp& __x){if (__n != 0) {if (size_type(_M_end_of_storage - _M_finish) >= __n) {_Tp __x_copy = __x;const size_type __elems_after = _M_finish - __position;iterator __old_finish = _M_finish;if (__elems_after > __n) {uninitialized_copy(_M_finish - __n, _M_finish, _M_finish);_M_finish += __n;copy_backward(__position, __old_finish - __n, __old_finish);fill(__position, __position + __n, __x_copy);}else {uninitialized_fill_n(_M_finish, __n - __elems_after, __x_copy);_M_finish += __n - __elems_after;uninitialized_copy(__position, __old_finish, _M_finish);_M_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 = _M_allocate(__len);iterator __new_finish = __new_start;__STL_TRY {__new_finish = uninitialized_copy(_M_start, __position, __new_start);__new_finish = uninitialized_fill_n(__new_finish, __n, __x);__new_finish= uninitialized_copy(__position, _M_finish, __new_finish);}__STL_UNWIND((destroy(__new_start,__new_finish), _M_deallocate(__new_start,__len)));destroy(_M_start, _M_finish);_M_deallocate(_M_start, _M_end_of_storage - _M_start);_M_start = __new_start;_M_finish = __new_finish;_M_end_of_storage = __new_start + __len;}}}轉(zhuǎn)載于:https://www.cnblogs.com/studentdeng/archive/2011/01/01/1923686.html
總結(jié)
以上是生活随笔為你收集整理的SGI STL 学习笔记二 vector的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sourcing Cockpit: 2.
- 下一篇: 家里路由器如何共享同一个ip