STL list
来源:互联网 发布:对网络教育行业的看法 编辑:程序博客网 时间:2024/06/10 23:21
源于 http://blog.csdn.net/xietingcandice/article/details/39993199
1:简介
在SGI STL中,list容器是一个循环的双向链表,它的内存空间效率较前文介绍的vector容器高。因为vector容器的内存空间是连续存储的,且在分配内存空间时,会分配额外的可用空间;而list容器的内存空间不一定是连续存储,内存之间是采用迭代器或节点指针进行连接,并且在插入或删除数据节点时,就配置或释放一个数据节点,并不会分配额外的内存空间,这两个操作过程都是常数时间。
与vector容器不同的是,list容器在进行插入操作或拼接操作时,迭代器并不会失效;且不能以普通指针作为迭代器,因为普通指针的+或-操作只能指向连续空间的后移地址或前移个地址,不能保证指向list的下一个节点,迭代器必须是双向迭代器,因为list容器具备有前移和后移的能力。
2:list的节点和list
在list容器中,list本身和list节点是分开设计的,list节点结构是存储数据和指向相邻节点的指针,和数据结构中的双向链表对应,但是注意他是环形的
- //以下是list链表节点的数据结构
- struct _List_node_base {
- _List_node_base* _M_next;//指向直接后继节点
- _List_node_base* _M_prev;//指向直接前驱节点
- };
- template <class _Tp>
- struct _List_node : public _List_node_base {
- _Tp _M_data;//节点存储的数据
- };
- //以下是双向链表list类的定义,分配器_Alloc默认为第二级配置器
- template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
- class list : protected _List_base<_Tp, _Alloc> {
- ...
- public:
- typedef _List_node<_Tp> _Node;
- protected:
- //定义指向链表节点指针
- _List_node<_Tp>* _M_node;
- ...
- };
3:list容器的迭代器
list容器的内存空间存储不一定是连续的,则不能用普通指针做为迭代器;list容器的迭代器是双向迭代器,这也是导致list容器的排序成员函数sort()不能使用STL算法中的排序函数(之后会专门进行介绍),因为STL中的排序算法接受的迭代器是随机访问迭代器;list容器在进行插入和拼接操作时迭代器不会失效
- //以下是链表List_iterator_base的迭代器
- struct _List_iterator_base {
- //数据类型
- typedef size_t size_type;
- typedef ptrdiff_t difference_type;
- //list迭代器的类型是双向迭代器bidirectional_iterator
- typedef bidirectional_iterator_tag iterator_category;
- //定义指向链表节点的指针
- _List_node_base* _M_node;
- //构造函数
- _List_iterator_base(_List_node_base* __x) : _M_node(__x) {}
- _List_iterator_base() {}
- //更新节点指针,指向直接前驱或直接后继节点
- void _M_incr() { _M_node = _M_node->_M_next; }
- void _M_decr() { _M_node = _M_node->_M_prev; }
- //操作符重载
- bool operator==(const _List_iterator_base& __x) const {
- return _M_node == __x._M_node;
- }
- bool operator!=(const _List_iterator_base& __x) const {
- return _M_node != __x._M_node;
- }
- };
- //以下是链表List_iterator的迭代器
- template<class _Tp, class _Ref, class _Ptr>
- struct _List_iterator : public _List_iterator_base {
- typedef _List_iterator<_Tp,_Tp&,_Tp*> iterator;
- typedef _List_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
- typedef _List_iterator<_Tp,_Ref,_Ptr> _Self;
- typedef _Tp value_type;
- typedef _Ptr pointer;
- typedef _Ref reference;
- typedef _List_node<_Tp> _Node;
- //构造函数
- _List_iterator(_Node* __x) : _List_iterator_base(__x) {}
- _List_iterator() {}
- _List_iterator(const iterator& __x) : _List_iterator_base(__x._M_node) {}
- //以下都是基本操作符的重载,取出节点数据
- reference operator*() const { return ((_Node*) _M_node)->_M_data; }
- #ifndef __SGI_STL_NO_ARROW_OPERATOR
- pointer operator->() const { return &(operator*()); }
- #endif /* __SGI_STL_NO_ARROW_OPERATOR */
- _Self& operator++() {
- this->_M_incr();
- return *this;
- }
- _Self operator++(int) {
- _Self __tmp = *this;
- this->_M_incr();
- return __tmp;
- }
- _Self& operator--() {
- this->_M_decr();
- return *this;
- }
- _Self operator--(int) {
- _Self __tmp = *this;
- this->_M_decr();
- return __tmp;
- }
- };
- #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
- //返回迭代器的类型
- inline bidirectional_iterator_tag
- iterator_category(const _List_iterator_base&)
- {
- return bidirectional_iterator_tag();
- }
- template <class _Tp, class _Ref, class _Ptr>
- inline _Tp*
- value_type(const _List_iterator<_Tp, _Ref, _Ptr>&)
- {
- return 0;
- }
- inline ptrdiff_t*
- distance_type(const _List_iterator_base&)
- {
- return 0;
- }
- #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
- //以下是双向链表list类的定义,分配器_Alloc默认为第二级配置器
- template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
- class list : protected _List_base<_Tp, _Alloc> {
- ...
- public:
- //**********************************************************************
- /***********************以下是构造函数**********************************
- //*******************默认构造函数***************************************
- explicit list( const Allocator& alloc = Allocator() );
- //**********************具有初值和大小的构造函数************************
- explicit list( size_type count,
- const T& value = T(),
- const Allocator& alloc = Allocator());
- list( size_type count,
- const T& value,
- const Allocator& alloc = Allocator());
- //**************只有大小的构造函数**************************************
- explicit list( size_type count );
- //************某个范围的值为初始值的构造函数****************************
- template< class InputIt >
- list( InputIt first, InputIt last,
- const Allocator& alloc = Allocator() );
- //************拷贝构造函数***********************************************
- list( const list& other );
- */
- //**********************************************************************
- //构造函数
- //链表的默认构造函数
- explicit list(const allocator_type& __a = allocator_type()) : _Base(__a) {}
- list(size_type __n, const _Tp& __value,
- const allocator_type& __a = allocator_type())
- : _Base(__a)
- { insert(begin(), __n, __value); }
- explicit list(size_type __n)
- : _Base(allocator_type())
- { insert(begin(), __n, _Tp()); }
- #ifdef __STL_MEMBER_TEMPLATES
- // We don't need any dispatching tricks here, because insert does all of
- // that anyway.
- template <class _InputIterator>
- list(_InputIterator __first, _InputIterator __last,
- const allocator_type& __a = allocator_type())
- : _Base(__a)
- { insert(begin(), __first, __last); }
- #else /* __STL_MEMBER_TEMPLATES */
- list(const _Tp* __first, const _Tp* __last,
- const allocator_type& __a = allocator_type())
- : _Base(__a)
- { this->insert(begin(), __first, __last); }
- list(const_iterator __first, const_iterator __last,
- const allocator_type& __a = allocator_type())
- : _Base(__a)
- { this->insert(begin(), __first, __last); }
- #endif /* __STL_MEMBER_TEMPLATES */
- list(const list<_Tp, _Alloc>& __x) : _Base(__x.get_allocator())
- { insert(begin(), __x.begin(), __x.end()); }//拷贝构造函数
- ~list() { }//析构函数
- //赋值操作
- list<_Tp, _Alloc>& operator=(const list<_Tp, _Alloc>& __x);
- //构造函数,析构函数,赋值操作 定义到此结束
- //*******************************************************************
- ...
- };
5:list的数据成员以及内存管理
如果让list结构中表示的node指向刻意置于尾端的空白节点,那么node就能够符合STL的前闭后开区间的要求
list缺省是使用alloc作为空间配置器,并且为此另外定义了list_node_allocator为的是以节点大小配置单位
- //以下是双向链表list类的定义,分配器_Alloc默认为第二级配置器
- template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
- class list : protected _List_base<_Tp, _Alloc> {
- // requirements:
- ...
- protected:
- //创建值为x的节点,并返回该节点的地址
- _Node* _M_create_node(const _Tp& __x)
- {
- _Node* __p = _M_get_node();//分配一个节点空间
- __STL_TRY {//把x值赋予指定的地址,即是data值
- _Construct(&__p->_M_data, __x);
- }
- __STL_UNWIND(_M_put_node(__p));
- return __p;//返回节点地址
- }
- //创建默认值的节点
- _Node* _M_create_node()
- {
- _Node* __p = _M_get_node();
- __STL_TRY {
- _Construct(&__p->_M_data);
- }
- __STL_UNWIND(_M_put_node(__p));
- return __p;
- }
- public:
- //以下是迭代器的定义
- iterator begin() { return (_Node*)(_M_node->_M_next); }
- const_iterator begin() const { return (_Node*)(_M_node->_M_next); }
- iterator end() { return _M_node; }
- const_iterator end() const { return _M_node; }
- 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()); }
- //是判断链表否为空链表
- bool empty() const { return _M_node->_M_next == _M_node; }
- //返回链表的大小
- size_type size() const {
- size_type __result = 0;
- //返回两个迭代器之间的距离
- distance(begin(), end(), __result);
- //返回链表的元素个数
- return __result;
- }
- size_type max_size() const { return size_type(-1); }
- //返回第一个节点数据的引用,reference相当于value_type&
- reference front() { return *begin(); }
- const_reference front() const { return *begin(); }
- //返回最后一个节点数据的引用
- reference back() { return *(--end()); }
- const_reference back() const { return *(--end()); }
- //交换链表容器的内容
- void swap(list<_Tp, _Alloc>& __x) { __STD::swap(_M_node, __x._M_node); }
- //**********************************************************************
- //*********************插入节点*****************************************
- /******************以下是插入节点函数的原型,也是公共接口**************
- //在指定的位置pos之前插入值为value的数据节点
- iterator insert( iterator pos, const T& value );
- iterator insert( const_iterator pos, const T& value );
- //在指定的位置pos之前插入n个值为value的数据节点
- void insert( iterator pos, size_type count, const T& value );
- iterator insert( const_iterator pos, size_type count, const T& value );
- //在指定的位置pos之前插入[first,last)之间的数据节点
- template< class InputIt >
- void insert( iterator pos, InputIt first, InputIt last);
- template< class InputIt >
- iterator insert( const_iterator pos, InputIt first, InputIt last );
- ***********************************************************************/
- /**在整个链表的操作中,插入操作是非常重要的,很多成员函数会调用该函数**/
- //***********************************************************************
- //在指定的位置插入初始值为x的节点
- iterator insert(iterator __position, const _Tp& __x) {
- //首先创建一个初始值为x的节点,并返回该节点的地址
- _Node* __tmp = _M_create_node(__x);
- //调整节点指针,把新节点插入到指定位置
- __tmp->_M_next = __position._M_node;
- __tmp->_M_prev = __position._M_node->_M_prev;
- __position._M_node->_M_prev->_M_next = __tmp;
- __position._M_node->_M_prev = __tmp;
- //返回新节点地址
- return __tmp;
- }
- //在指定的位置插入为默认值的节点
- iterator insert(iterator __position) { return insert(__position, _Tp()); }
- //在指定位置插入n个初始值为x的节点
- void insert(iterator __pos, size_type __n, const _Tp& __x)
- { _M_fill_insert(__pos, __n, __x); }
- void _M_fill_insert(iterator __pos, size_type __n, const _Tp& __x);
- #ifdef __STL_MEMBER_TEMPLATES
- // Check whether it's an integral type. If so, it's not an iterator.
- //这里采用__type_traits技术
- //在指定位置插入指定范围内的数据
- //首先判断输入迭代器类型_InputIterator是否为整数类型
- template <class _InputIterator>
- void insert(iterator __pos, _InputIterator __first, _InputIterator __last) {
- typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
- _M_insert_dispatch(__pos, __first, __last, _Integral());
- }
- //若输入迭代器类型_InputIterator是为整数类型,调用此函数
- template<class _Integer>
- void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
- __true_type) {
- _M_fill_insert(__pos, (size_type) __n, (_Tp) __x);
- }
- //若输入迭代器类型_InputIterator是不为整数类型,调用此函数
- template <class _InputIterator>
- void _M_insert_dispatch(iterator __pos,
- _InputIterator __first, _InputIterator __last,
- __false_type);
- #else /* __STL_MEMBER_TEMPLATES */
- void insert(iterator __position, const _Tp* __first, const _Tp* __last);
- void insert(iterator __position,
- const_iterator __first, const_iterator __last);
- #endif /* __STL_MEMBER_TEMPLATES */
- //在链表头插入节点
- void push_front(const _Tp& __x) { insert(begin(), __x); }
- void push_front() {insert(begin());}
- //在链表尾插入节点
- void push_back(const _Tp& __x) { insert(end(), __x); }
- void push_back() {insert(end());}
- //***********************************************************
- //********************在指定位置删除节点*********************
- //********************以下是删除节点的公共接口***************
- /************************************************************
- //删除指定位置pos的节点
- iterator erase( iterator pos );
- iterator erase( const_iterator pos );
- //删除指定范围[first,last)的数据节点
- iterator erase( iterator first, iterator last );
- iterator erase( const_iterator first, const_iterator last );
- ************************************************************/
- //***********************************************************
- //在指定位置position删除节点,并返回直接后继节点的地址
- iterator erase(iterator __position) {
- //调整前驱和后继节点的位置
- _List_node_base* __next_node = __position._M_node->_M_next;
- _List_node_base* __prev_node = __position._M_node->_M_prev;
- _Node* __n = (_Node*) __position._M_node;
- __prev_node->_M_next = __next_node;
- __next_node->_M_prev = __prev_node;
- _Destroy(&__n->_M_data);
- _M_put_node(__n);
- return iterator((_Node*) __next_node);
- }
- //删除两个迭代器之间的节点
- iterator erase(iterator __first, iterator __last);
- //清空链表,这里是调用父类的clear()函数
- void clear() { _Base::clear(); }
- //调整链表的大小
- void resize(size_type __new_size, const _Tp& __x);
- void resize(size_type __new_size) { this->resize(__new_size, _Tp()); }
- //取出第一个数据节点
- void pop_front() { erase(begin()); }
- //取出最后一个数据节点
- void pop_back() {
- iterator __tmp = end();
- erase(--__tmp);
- }
- public:
- // assign(), a generalized assignment member function. Two
- // versions: one that takes a count, and one that takes a range.
- // The range version is a member template, so we dispatch on whether
- // or not the type is an integer.
- /*********************************************************************
- //assign()函数的两个版本原型,功能是在已定义的list容器填充值
- void assign( size_type count, const T& value );
- template< class InputIt >
- void assign( InputIt first, InputIt last );
- //*******************************************************************
- 例子:
- #include <list>
- #include <iostream>
- int main()
- {
- std::list<char> characters;
- //若定义characters时并初始化为字符b,下面的填充操作一样有效
- //std::list<char>characters(5,'b')
- characters.assign(5, 'a');
- for (char c : characters) {
- std::cout << c << ' ';
- }
- return 0;
- }
- 输出结果:a a a a a
- *********************************************************************/
- //这里是第一个版本void assign( size_type count, const T& value );
- void assign(size_type __n, const _Tp& __val) { _M_fill_assign(__n, __val); }
- //这里为什么要把_M_fill_assign这个函数放在public呢??保护起来不是更好吗??
- void _M_fill_assign(size_type __n, const _Tp& __val);
- #ifdef __STL_MEMBER_TEMPLATES
- //以下是针对assign()函数的第二个版本
- /*
- template< class InputIt >
- void assign( InputIt first, InputIt last );
- 这里有偏特化的现象,判断输入数据类型是否为整数型别
- */
- template <class _InputIterator>
- void assign(_InputIterator __first, _InputIterator __last) {
- typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
- _M_assign_dispatch(__first, __last, _Integral());
- }
- //若输入数据类型为整数型别,则派送到此函数
- template <class _Integer>
- void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
- { _M_fill_assign((size_type) __n, (_Tp) __val); }
- //若输入数据类型不是整数型别,则派送到此函数
- template <class _InputIterator>
- void _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
- __false_type);
- #endif /* __STL_MEMBER_TEMPLATES */
- //assign()函数定义结束
- //*****************************************************************
- protected:
- //把区间[first,last)的节点数据插入到指定节点position之前,position不能在区间内部
- //这个函数是list类的protected属性,不是公共接口,只为list类成员服务
- //为下面拼接函数void splice()服务
- void transfer(iterator __position, iterator __first, iterator __last) {
- if (__position != __last) {
- // Remove [first, last) from its old position.
- __last._M_node->_M_prev->_M_next = __position._M_node;
- __first._M_node->_M_prev->_M_next = __last._M_node;
- __position._M_node->_M_prev->_M_next = __first._M_node;
- // Splice [first, last) into its new position.
- _List_node_base* __tmp = __position._M_node->_M_prev;
- __position._M_node->_M_prev = __last._M_node->_M_prev;
- __last._M_node->_M_prev = __first._M_node->_M_prev;
- __first._M_node->_M_prev = __tmp;
- }
- }
- public:
- //**********************************************************
- //*******************拼接操作对外接口***********************
- //把链表拼接到当前链表指定位置position之前
- /*void splice(const_iterator pos, list& other);
- //把it在链表other所指的位置拼接到当前链表pos之前,it和pos可指向同一链表
- void splice(const_iterator pos, list& other, const_iterator it);
- //把链表other的节点范围[first,last)拼接在当前链表所指定的位置pos之前
- //[first,last)和pos可指向同一链表
- void splice(const_iterator pos, list& other,
- const_iterator first, const_iterator last);
- *************************************************************/
- //**********************************************************
- //将链表x拼接到当前链表的指定位置position之前
- //这里x和*this必须不同,即是两个不同的链表
- void splice(iterator __position, list& __x) {
- if (!__x.empty())
- this->transfer(__position, __x.begin(), __x.end());
- }
- //将i所指向的节点拼接到position所指位置之前
- //注意:i和position可以指向同一个链表
- void splice(iterator __position, list&, iterator __i) {
- iterator __j = __i;
- ++__j;
- //若i和position指向同一个链表,且指向同一位置
- //或者i和position指向同一个链表,且就在position的直接前驱位置
- //针对以上这两种情况,不做任何操作
- if (__position == __i || __position == __j) return;
- //否则,进行拼接操作
- this->transfer(__position, __i, __j);
- }
- //将范围[first,last)内所有节点拼接到position所指位置之前
- //注意:[first,last)和position可指向同一个链表,
- //但是position不能在[first,last)范围之内
- void splice(iterator __position, list&, iterator __first, iterator __last) {
- if (__first != __last)
- this->transfer(__position, __first, __last);
- }
- //以下是成员函数声明,定义在list类外实现
- //************************************************************
- //删除链表中值等于value的所有节点
- void remove(const _Tp& __value);
- //删除连续重复的元素节点,使之唯一
- //注意:是连续的重复元素
- void unique();
- //合并两个已排序的链表
- void merge(list& __x);
- //反转链表容器的内容
- void reverse();
- //按升序排序链表内容
- void sort();
- #ifdef __STL_MEMBER_TEMPLATES
- template <class _Predicate> void remove_if(_Predicate);
- template <class _BinaryPredicate> void unique(_BinaryPredicate);
- template <class _StrictWeakOrdering> void merge(list&, _StrictWeakOrdering);
- template <class _StrictWeakOrdering> void sort(_StrictWeakOrdering);
- #endif /* __STL_MEMBER_TEMPLATES */
- };
细致介绍一个在pos位置插入一个节点的算法,和在数据结构中操作双向链表一样
- //*********************插入节点*****************************************
- /******************以下是插入节点函数的原型,也是公共接口**************
- //在指定的位置pos之前插入值为value的数据节点
- iterator insert( iterator pos, const T& value );
- iterator insert( const_iterator pos, const T& value );
- //在指定的位置pos之前插入n个值为value的数据节点
- void insert( iterator pos, size_type count, const T& value );
- iterator insert( const_iterator pos, size_type count, const T& value );
- //在指定的位置pos之前插入[first,last)之间的数据节点
- template< class InputIt >
- void insert( iterator pos, InputIt first, InputIt last);
- template< class InputIt >
- iterator insert( const_iterator pos, InputIt first, InputIt last );
- ***********************************************************************/
- /**在整个链表的操作中,插入操作是非常重要的,很多成员函数会调用该函数**/
- /***********************************************************************
- //在指定的位置插入初始值为x的节点
- iterator insert(iterator __position, const _Tp& __x) {
- //首先创建一个初始值为x的节点,并返回该节点的地址
- _Node* __tmp = _M_create_node(__x);
- //调整节点指针,把新节点插入到指定位置
- __tmp->_M_next = __position._M_node;
- __tmp->_M_prev = __position._M_node->_M_prev;
- __position._M_node->_M_prev->_M_next = __tmp;
- __position._M_node->_M_prev = __tmp;
- //返回新节点地址
- return __tmp;
- }
- STL--list
- STL--list
- STL list
- STL list
- STL----list
- STL list
- stl-list
- stl list
- STL--list
- STL list
- stl list
- STL---List
- STL list
- STL list
- STL list
- STL list
- STL list
- STL List
- android下判断一个应用app是否是系统rom集成的方法
- 3117 高精度练习之乘法
- 推荐给每个找工作的IT毕业生--打鸡血书
- ANDROID 中设计模式的采用--创建型模式
- Spring AMQP消息监听容器配置
- STL list
- 举例说明 public private protected 的区别
- N-gram统计语言模型(总结)
- android的PhotoShop
- 【转】c# winform TreeView控件中实现拖拽的功能
- MVC 检测用户是否已经登录
- ECShop出现Strict Standards: Only variables should be passed by reference in的解决方法
- vijos P1121 马拦过河卒
- 第十二周项目四(1)童氏银行框架版