日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL浅析——序列式容器vector的数据结构

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL浅析——序列式容器vector的数据结构 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  vecotr 一詞原來的意思是:矢量,向量,航向,顧名思義指的就是類似于數組的一個存儲數據的序列,因此所采用的數據結構非常簡單:連續的線性空間,它以兩個迭代器 _M_start 和 _M_finish 分別指向配置得來的連續線性空間中目前已被使用的范圍,并以迭代器 _M_end_of_storage 指向整塊連續空間的尾端:

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;};#endif /* __STL_USE_STD_ALLOCATORS */template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) > class vector : protected _Vector_base<_Tp, _Alloc> { private:typedef _Vector_base<_Tp, _Alloc> _Base;protected: #ifdef __STL_HAS_NAMESPACESusing _Base::_M_allocate;using _Base::_M_deallocate;using _Base::_M_start;using _Base::_M_finish;using _Base::_M_end_of_storage; #endif /* __STL_HAS_NAMESPACES */
};

  為了降低空間配置時的速度成本,vector 實際配置的大小可能比客戶端需求量更大,以備將來可能的擴充,這便是容量(capacity)的概念,當容器滿載的時候,整個 vector 就必須另外尋找內存存數據。

  使用_M_start,_M_end,_M_end_of_storage三個迭代器,便能輕易地提供首尾標識、大小、容量、空容器判斷,標注運算子、最前端元素值,最后端元素值等功能。

  

template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) > class vector : protected _Vector_base<_Tp, _Alloc> {... public:iterator begin() { return _M_start; }const_iterator begin() const { return _M_start; }iterator end() { return _M_finish; }const_iterator end() const { return _M_finish; }reverse_iterator rbegin(){ return reverse_iterator(end()); }const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }reverse_iterator rend(){ return reverse_iterator(begin()); }const_reverse_iterator rend() const{ return const_reverse_iterator(begin()); }size_type size() const{ return size_type(end() - begin()); }size_type max_size() const{ return size_type(-1) / sizeof(_Tp); }size_type capacity() const{ return size_type(_M_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); } ...reference front() { return *begin(); }const_reference front() const { return *begin(); }reference back() { return *(end() - 1); }const_reference back() const { return *(end() - 1); }
... };

  如下為vector示意圖:

轉載于:https://www.cnblogs.com/Forever-Road/p/6831336.html

總結

以上是生活随笔為你收集整理的STL浅析——序列式容器vector的数据结构的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。