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重新封裝而成,在技術上被歸類為一種配接器(adapter)。
Vector
- Vector 的數據為動態空間,隨著元素的加入。內部會通過機制自行擴充空間,以容納新元素。
- Vector 的效率,在于對大小的控制,重新分配時數據移動效率。當空間不足時,vector會選擇策略擴充容量。
- Vector resize之后,很可能使所有迭代器均失效。 插入后,插入點之前的Iterator 有效,其他則無效。eraser迭代器失效。
Vector實現
Vector 實現比較簡單。這里僅僅作為打開SGI STL的敲門磚。
我這里的SGI STL 對vector有進行了進一步封裝。在頭文件中,也給出了我們的解釋。
// 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.
這里根據 宏 __STL_USE_STD_ALLOCATORS 來決定是否資源分配器。如果定義了__STL_USE_STD_ALLOCATORS, 則使用allocator< _Tp >,否則為alloc
//這里的 _Vector_base 為我們隱藏了 使用STL 標準分配器,和SGI 自己特有的分配器之間的不同 //我們現在先把這里具體的分配細節透明。 //這是,使用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 就是一個指針。若是定義
vector<int>:: iterator iter1;
vector<RECT>:: iterator iter2;
那么,Iter1 其實,就是int *, iter2其實就是RECT * 。
看一下,部分的vector 函數,也是我們常常使用的。
Vector 的數據,什么時候被釋放。我們需要看析構函數。
~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個偏特化版本。可以看出,在一些特殊情況下,我們找到了最快速的方法。什么也不干。
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&){//這里,僅僅構造一個臨時對象(準確說是指針)來做返回值,事實上,我們不關心他到底是個什么,只是關心她的類型。//用這個類型來激發函數重載,所以,用0來構造也無妨。return static_cast<typename iterator_traits<_Iter>::value_type*>(0);}template <class _ForwardIterator, class _Tp>inline void__destroy(_ForwardIterator __first, _ForwardIterator __last, _Tp*)//這里多了一個接受這個類型對象參數{//根據這個類型_Tp,我們根據__type_traits<_Tp>,找到了這個類型是否有has_trivial_destructor。typedef typename __type_traits<_Tp>::has_trivial_destructor _Trivial_destructor;//然后構造一個臨時的對象來激發函數重載。__destroy_aux(__first, __last, _Trivial_destructor());}//下面2個便是特化后的結果。//__false_type,我們老老實實的該干什么干什么。template <class _ForwardIterator>inline void__destroy_aux(_ForwardIterator __first, _ForwardIterator __last, __false_type){for ( ; __first != __last; ++__first)destroy(&*__first);}//__true_type 我們實在是沒有這個必要和他糾結了。template <class _ForwardIterator>inline void __destroy_aux(_ForwardIterator, _ForwardIterator, __true_type) {}這是可能懷疑,內存到那里釋放呢? 別忘了,我們的vector 是繼承自_Vector_base,內存釋放,管理都隱藏在他那里。
~_Vector_base() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); }
這里才真正的執行內存的回收。但是這里又涉及到了SGI STL 的內存管理,這部分是給操作系統,還是給內存池呢?
在沒有研究過細致的內存管理之前。我們還是將這里透明吧。
基本操作
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 是全局函數,操作簡單,同樣有多個特化版本。Vector 和一般數組的刪除動作一樣,將后面元素一個個往前搬。最后修改個數。
插入 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;}}的確很簡單,和我們在學校學的并沒有什么大的不同,只是在對新增元素的構造上不同。
construct(__new_finish),
construct(_M_finish, *(_M_finish - 1));
以上construct是全局函數,同樣有特化版本。將類的構造分成,資源分配 + 構造函數,來做到提高效率。這樣在大量數據上效果應該很明顯,并沒有具體測試。
?
對一次插入大量元素時,vector 的策略是。if (插入元素個數 == 0 ) return if (判斷容量是否足夠){if (插入點后的元素個數 > 待插入元素個數){按照最后一個元素,構造插入元素個數個元素。向插入點數據向后搬運。移動指針。將待插入元素順次插入。}else{先以__x構造元素,在不需要移動位置的地方。將原來的元素,移動到最后。在插入位置處,以__x構造元素。}}else{根據策略分配空間(這里至少PJ 和SGI的策略不同,這里應該和不同的內存管理策略有關)將插入點之前的原有的數據復制到新空間依次復制新元素到新空間。依次復制原來數據到新空間} 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;}}}轉載于:https://www.cnblogs.com/studentdeng/archive/2011/01/01/1923686.html
總結
以上是生活随笔為你收集整理的SGI STL 学习笔记二 vector的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sourcing Cockpit: 2.
- 下一篇: 47、Windows驱动程序模型笔记(五