设为首页 加入收藏

TOP

C++STL源码学习(之slist篇)(二)
2015-07-20 17:30:09 来源: 作者: 【 】 浏览:24
Tags:STL 源码 学习 slist篇
()); } _Self& operator++() { _M_incr(); return *this; } _Self operator++(int) { _Self __tmp = *this; _M_incr(); return __tmp; } }; inline ptrdiff_t* distance_type(const _Slist_iterator_base&) { return 0; } inline forward_iterator_tag iterator_category(const _Slist_iterator_base&) { return forward_iterator_tag(); } template inline _Tp* value_type(const _Slist_iterator<_Tp, _Ref, _Ptr>&) { return 0; } template struct _Slist_base { typedef _Alloc allocator_type; allocator_type get_allocator() const { return allocator_type(); } _Slist_base(const allocator_type&) { _M_head._M_next = 0; } ~_Slist_base() { _M_erase_after(&_M_head, 0); } ///清空链表 protected: typedef simple_alloc<_Slist_node<_Tp>, _Alloc> _Alloc_type; _Slist_node<_Tp>* _M_get_node() { return _Alloc_type::allocate(1); } void _M_put_node(_Slist_node<_Tp>* __p) { _Alloc_type::deallocate(__p, 1); } ///删除__pos->_M_next _Slist_node_base* _M_erase_after(_Slist_node_base* __pos) { _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next); _Slist_node_base* __next_next = __next->_M_next; __pos->_M_next = __next_next; destroy(&__next->_M_data); _M_put_node(__next); return __next_next; } _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*); protected: _Slist_node_base _M_head; ///不存储任何数据元素的头结点 }; ///删除(__before_first,__last_node) template _Slist_node_base* _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first, _Slist_node_base* __last_node) { _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next); while (__cur != __last_node) { _Slist_node<_Tp>* __tmp = __cur; __cur = (_Slist_node<_Tp>*) __cur->_M_next; destroy(&__tmp->_M_data); _M_put_node(__tmp); } __before_first->_M_next = __last_node; return __last_node; } template class slist : private _Slist_base<_Tp,_Alloc> { __STL_CLASS_REQUIRES(_Tp, _Assignable); private: typedef _Slist_base<_Tp,_Alloc> _Base; public: typedef _Tp value_type; typedef value_type* pointer; typedef const value_type* const_pointer; typedef value_type& reference; typedef const value_type& const_reference; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator; typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator; typedef typename _Base::allocator_type allocator_type; allocator_type get_allocator() const { return _Base::get_allocator(); } private: typedef _Slist_node<_Tp> _Node; typedef _Slist_node_base _Node_base; typedef _Slist_iterator_base _Iterator_base; ///构造一个数据元素为x的结点 _Node* _M_create_node(const value_type& __x) { _Node* __node = this->_M_get_node(); try { construct(&__node->_M_data, __x); __node->_M_next = 0; }catch(...){ this->_M_put_node(__node); } return __node; } _Node* _M_create_node() { _Node* __node = this->_M_get_node(); try { construct(&__node->_M_data); __node->_M_next = 0; }catch(...){ this->_M_put_node(__node); } return __node; } public: explicit slist(const allocator_type& __a = allocator_type()) : _Base(__a) {} slist(size_type __n, const value_type& __x, const allocator_type& __a = allocator_type()) : _Base(__a) { _M_insert_after_fill(&this->_M_head, __n, __x); } explicit slist(size_type __n) : _Base(allocator_type()) { _M_insert_after_fill(&this->_M_head, __n, value_type()); } /// We don't need any dispatching tricks here, because _M_insert_after_range /// already does them. template slist(_InputIterator __first, _InputIterator __last, const allocator_type& __a = allocator_type()) : _Base(__a) { _M
首页 上一页 1 2 3 4 5 6 下一页 尾页 2/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDoj-1018-Big Number 下一篇springmvc+easyui实现透视表-动态..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·求navicat for mysql (2025-12-26 13:21:33)
·有哪位大哥推荐一下m (2025-12-26 13:21:30)
·MySQL下载与安装教程 (2025-12-26 13:21:26)
·Linux_百度百科 (2025-12-26 12:51:52)
·Shell 流程控制 | 菜 (2025-12-26 12:51:49)