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节点结构是存储数据和指向相邻节点的指针,和数据结构中的双向链表对应,但是注意他是环形的

  1. //以下是list链表节点的数据结构  
  2. struct _List_node_base {  
  3.   _List_node_base* _M_next;//指向直接后继节点  
  4.   _List_node_base* _M_prev;//指向直接前驱节点  
  5. };  
  6.   
  7. template <class _Tp>  
  8. struct _List_node : public _List_node_base {  
  9.   _Tp _M_data;//节点存储的数据  
  10. };  
list本身的数据结构是只有一个指向链表节点的指针,因为list容器是循环双向链表,一个节点则足够遍历整个链表

  1. //以下是双向链表list类的定义,分配器_Alloc默认为第二级配置器  
  2. template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >  
  3. class list : protected _List_base<_Tp, _Alloc> {   
  4.      ...  
  5. public:  
  6.     typedef _List_node<_Tp> _Node;  
  7. protected:  
  8.     //定义指向链表节点指针  
  9.     _List_node<_Tp>* _M_node;  
  10.     ...  
  11. };  

3:list容器的迭代器

 list容器的内存空间存储不一定是连续的,则不能用普通指针做为迭代器;list容器的迭代器是双向迭代器,这也是导致list容器的排序成员函数sort()不能使用STL算法中的排序函数(之后会专门进行介绍),因为STL中的排序算法接受的迭代器是随机访问迭代器;list容器在进行插入和拼接操作时迭代器不会失效

  1. //以下是链表List_iterator_base的迭代器  
  2. struct _List_iterator_base {  
  3.     //数据类型  
  4.   typedef size_t                     size_type;  
  5.   typedef ptrdiff_t                  difference_type;  
  6.   //list迭代器的类型是双向迭代器bidirectional_iterator  
  7.   typedef bidirectional_iterator_tag iterator_category;  
  8.   
  9.   //定义指向链表节点的指针  
  10.   _List_node_base* _M_node;  
  11.   
  12.   //构造函数  
  13.   _List_iterator_base(_List_node_base* __x) : _M_node(__x) {}  
  14.   _List_iterator_base() {}  
  15.   
  16.   //更新节点指针,指向直接前驱或直接后继节点  
  17.   void _M_incr() { _M_node = _M_node->_M_next; }  
  18.   void _M_decr() { _M_node = _M_node->_M_prev; }  
  19.   
  20.   //操作符重载  
  21.   bool operator==(const _List_iterator_base& __x) const {  
  22.     return _M_node == __x._M_node;  
  23.   }  
  24.   bool operator!=(const _List_iterator_base& __x) const {  
  25.     return _M_node != __x._M_node;  
  26.   }  
  27. };    
  28.   
  29. //以下是链表List_iterator的迭代器  
  30. template<class _Tp, class _Ref, class _Ptr>  
  31. struct _List_iterator : public _List_iterator_base {  
  32.   typedef _List_iterator<_Tp,_Tp&,_Tp*>             iterator;  
  33.   typedef _List_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;  
  34.   typedef _List_iterator<_Tp,_Ref,_Ptr>             _Self;  
  35.   
  36.   typedef _Tp value_type;  
  37.   typedef _Ptr pointer;  
  38.   typedef _Ref reference;  
  39.   typedef _List_node<_Tp> _Node;  
  40.   
  41.   //构造函数  
  42.   _List_iterator(_Node* __x) : _List_iterator_base(__x) {}  
  43.   _List_iterator() {}  
  44.   _List_iterator(const iterator& __x) : _List_iterator_base(__x._M_node) {}  
  45.   
  46.   //以下都是基本操作符的重载,取出节点数据  
  47.   reference operator*() const { return ((_Node*) _M_node)->_M_data; }  
  48.   
  49. #ifndef __SGI_STL_NO_ARROW_OPERATOR  
  50.   pointer operator->() const { return &(operator*()); }  
  51. #endif /* __SGI_STL_NO_ARROW_OPERATOR */  
  52.   
  53.   _Self& operator++() {   
  54.     this->_M_incr();  
  55.     return *this;  
  56.   }  
  57.   _Self operator++(int) {   
  58.     _Self __tmp = *this;  
  59.     this->_M_incr();  
  60.     return __tmp;  
  61.   }  
  62.   _Self& operator--() {   
  63.     this->_M_decr();  
  64.     return *this;  
  65.   }  
  66.   _Self operator--(int) {   
  67.     _Self __tmp = *this;  
  68.     this->_M_decr();  
  69.     return __tmp;  
  70.   }  
  71. };  
  72.   
  73. #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION  
  74.   
  75. //返回迭代器的类型  
  76. inline bidirectional_iterator_tag  
  77. iterator_category(const _List_iterator_base&)  
  78. {  
  79.   return bidirectional_iterator_tag();  
  80. }  
  81.   
  82. template <class _Tp, class _Ref, class _Ptr>  
  83. inline _Tp*  
  84. value_type(const _List_iterator<_Tp, _Ref, _Ptr>&)  
  85. {  
  86.   return 0;  
  87. }  
  88.   
  89. inline ptrdiff_t*  
  90. distance_type(const _List_iterator_base&)  
  91. {  
  92.   return 0;  
  93. }  
  94.   
  95. #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */  


4:list序列的构造函数

  1. //以下是双向链表list类的定义,分配器_Alloc默认为第二级配置器  
  2. template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >  
  3. class list : protected _List_base<_Tp, _Alloc> {   
  4.      ...  
  5. public:  
  6.  //**********************************************************************  
  7.   /***********************以下是构造函数**********************************  
  8.   //*******************默认构造函数***************************************  
  9.     explicit list( const Allocator& alloc = Allocator() );  
  10.   //**********************具有初值和大小的构造函数************************  
  11.     explicit list( size_type count,  
  12.                const T& value = T(),  
  13.                const Allocator& alloc = Allocator());  
  14.          list( size_type count,  
  15.                const T& value,  
  16.                const Allocator& alloc = Allocator());  
  17.   //**************只有大小的构造函数**************************************  
  18.     explicit list( size_type count );  
  19.   //************某个范围的值为初始值的构造函数****************************  
  20.     templateclass InputIt >  
  21.     list( InputIt first, InputIt last,  
  22.       const Allocator& alloc = Allocator() );  
  23.  //************拷贝构造函数***********************************************  
  24.     list( const list& other );  
  25.   */  
  26.   //**********************************************************************  
  27.   //构造函数  
  28.   //链表的默认构造函数  
  29.   explicit list(const allocator_type& __a = allocator_type()) : _Base(__a) {}  
  30.   list(size_type __n, const _Tp& __value,  
  31.        const allocator_type& __a = allocator_type())  
  32.     : _Base(__a)  
  33.     { insert(begin(), __n, __value); }  
  34.   explicit list(size_type __n)  
  35.     : _Base(allocator_type())  
  36.     { insert(begin(), __n, _Tp()); }  
  37.   
  38. #ifdef __STL_MEMBER_TEMPLATES  
  39.   
  40.   // We don't need any dispatching tricks here, because insert does all of  
  41.   // that anyway.    
  42.   template <class _InputIterator>  
  43.   list(_InputIterator __first, _InputIterator __last,  
  44.        const allocator_type& __a = allocator_type())  
  45.     : _Base(__a)  
  46.     { insert(begin(), __first, __last); }  
  47.   
  48. #else /* __STL_MEMBER_TEMPLATES */  
  49.   
  50.   list(const _Tp* __first, const _Tp* __last,  
  51.        const allocator_type& __a = allocator_type())  
  52.     : _Base(__a)  
  53.     { this->insert(begin(), __first, __last); }  
  54.   list(const_iterator __first, const_iterator __last,  
  55.        const allocator_type& __a = allocator_type())  
  56.     : _Base(__a)  
  57.     { this->insert(begin(), __first, __last); }  
  58.   
  59. #endif /* __STL_MEMBER_TEMPLATES */  
  60.   list(const list<_Tp, _Alloc>& __x) : _Base(__x.get_allocator())  
  61.     { insert(begin(), __x.begin(), __x.end()); }//拷贝构造函数  
  62.   
  63.   ~list() { }//析构函数  
  64.   
  65.   //赋值操作  
  66.   list<_Tp, _Alloc>& operator=(const list<_Tp, _Alloc>& __x);  
  67.   //构造函数,析构函数,赋值操作 定义到此结束  
  68.   //*******************************************************************  
  69.     ...  
  70. };  

5:list的数据成员以及内存管理

如果让list结构中表示的node指向刻意置于尾端的空白节点,那么node就能够符合STL的前闭后开区间的要求

list缺省是使用alloc作为空间配置器,并且为此另外定义了list_node_allocator为的是以节点大小配置单位

  1. //以下是双向链表list类的定义,分配器_Alloc默认为第二级配置器  
  2. template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >  
  3. class list : protected _List_base<_Tp, _Alloc> {  
  4.   // requirements:  
  5.   
  6.     ...  
  7.   
  8. protected:  
  9.     //创建值为x的节点,并返回该节点的地址  
  10.   _Node* _M_create_node(const _Tp& __x)  
  11.   {  
  12.     _Node* __p = _M_get_node();//分配一个节点空间  
  13.     __STL_TRY {//把x值赋予指定的地址,即是data值  
  14.       _Construct(&__p->_M_data, __x);  
  15.     }  
  16.     __STL_UNWIND(_M_put_node(__p));  
  17.     return __p;//返回节点地址  
  18.   }  
  19.   
  20.   //创建默认值的节点  
  21.   _Node* _M_create_node()  
  22.   {  
  23.     _Node* __p = _M_get_node();  
  24.     __STL_TRY {  
  25.       _Construct(&__p->_M_data);  
  26.     }  
  27.     __STL_UNWIND(_M_put_node(__p));  
  28.     return __p;  
  29.   }  
  30.   
  31. public:  
  32.       
  33.   
  34.   //以下是迭代器的定义  
  35.   iterator begin()             { return (_Node*)(_M_node->_M_next); }  
  36.   const_iterator begin() const { return (_Node*)(_M_node->_M_next); }  
  37.   
  38.   iterator end()             { return _M_node; }  
  39.   const_iterator end() const { return _M_node; }  
  40.   
  41.   reverse_iterator rbegin()   
  42.     { return reverse_iterator(end()); }  
  43.   const_reverse_iterator rbegin() const   
  44.     { return const_reverse_iterator(end()); }  
  45.   
  46.   reverse_iterator rend()  
  47.     { return reverse_iterator(begin()); }  
  48.   const_reverse_iterator rend() const  
  49.     { return const_reverse_iterator(begin()); }  
  50.   
  51.   //是判断链表否为空链表  
  52.   bool empty() const { return _M_node->_M_next == _M_node; }  
  53.    
  54.   //返回链表的大小  
  55.   size_type size() const {  
  56.     size_type __result = 0;  
  57.     //返回两个迭代器之间的距离  
  58.     distance(begin(), end(), __result);  
  59.     //返回链表的元素个数  
  60.     return __result;  
  61.   }  
  62.   size_type max_size() const { return size_type(-1); }  
  63.   
  64.   //返回第一个节点数据的引用,reference相当于value_type&  
  65.   reference front() { return *begin(); }  
  66.   const_reference front() const { return *begin(); }  
  67.   //返回最后一个节点数据的引用  
  68.   reference back() { return *(--end()); }  
  69.   const_reference back() const { return *(--end()); }  
  70.   
  71.   //交换链表容器的内容  
  72.   void swap(list<_Tp, _Alloc>& __x) { __STD::swap(_M_node, __x._M_node); }  
  73.   
  74.  //**********************************************************************  
  75.  //*********************插入节点*****************************************  
  76.   /******************以下是插入节点函数的原型,也是公共接口**************  
  77.     //在指定的位置pos之前插入值为value的数据节点  
  78.     iterator insert( iterator pos, const T& value );  
  79.     iterator insert( const_iterator pos, const T& value );  
  80.   
  81.     //在指定的位置pos之前插入n个值为value的数据节点  
  82.     void insert( iterator pos, size_type count, const T& value );  
  83.     iterator insert( const_iterator pos, size_type count, const T& value );  
  84.   
  85.     //在指定的位置pos之前插入[first,last)之间的数据节点  
  86.     templateclass InputIt >  
  87.     void insert( iterator pos, InputIt first, InputIt last);  
  88.     templateclass InputIt >  
  89.     iterator insert( const_iterator pos, InputIt first, InputIt last );  
  90.   ***********************************************************************/  
  91.   /**在整个链表的操作中,插入操作是非常重要的,很多成员函数会调用该函数**/  
  92. //***********************************************************************  
  93.   //在指定的位置插入初始值为x的节点  
  94.   iterator insert(iterator __position, const _Tp& __x) {  
  95.       //首先创建一个初始值为x的节点,并返回该节点的地址  
  96.     _Node* __tmp = _M_create_node(__x);  
  97.     //调整节点指针,把新节点插入到指定位置  
  98.     __tmp->_M_next = __position._M_node;  
  99.     __tmp->_M_prev = __position._M_node->_M_prev;  
  100.     __position._M_node->_M_prev->_M_next = __tmp;  
  101.     __position._M_node->_M_prev = __tmp;  
  102.     //返回新节点地址  
  103.     return __tmp;  
  104.   }  
  105.   //在指定的位置插入为默认值的节点  
  106.   iterator insert(iterator __position) { return insert(__position, _Tp()); }  
  107.   
  108.   //在指定位置插入n个初始值为x的节点  
  109.   void insert(iterator __pos, size_type __n, const _Tp& __x)  
  110.     { _M_fill_insert(__pos, __n, __x); }  
  111.   void _M_fill_insert(iterator __pos, size_type __n, const _Tp& __x);   
  112.   
  113. #ifdef __STL_MEMBER_TEMPLATES  
  114.   // Check whether it's an integral type.  If so, it's not an iterator.  
  115.   //这里采用__type_traits技术  
  116.    
  117.    //在指定位置插入指定范围内的数据  
  118.   //首先判断输入迭代器类型_InputIterator是否为整数类型  
  119.   template <class _InputIterator>  
  120.   void insert(iterator __pos, _InputIterator __first, _InputIterator __last) {  
  121.     typedef typename _Is_integer<_InputIterator>::_Integral _Integral;  
  122.     _M_insert_dispatch(__pos, __first, __last, _Integral());  
  123.   }  
  124.   
  125.     
  126.   //若输入迭代器类型_InputIterator是为整数类型,调用此函数  
  127.   template<class _Integer>  
  128.   void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,  
  129.                           __true_type) {  
  130.     _M_fill_insert(__pos, (size_type) __n, (_Tp) __x);  
  131.   }  
  132.   
  133.   //若输入迭代器类型_InputIterator是不为整数类型,调用此函数  
  134.   template <class _InputIterator>  
  135.   void _M_insert_dispatch(iterator __pos,  
  136.                           _InputIterator __first, _InputIterator __last,  
  137.                           __false_type);  
  138.   
  139.  #else /* __STL_MEMBER_TEMPLATES */  
  140.   void insert(iterator __position, const _Tp* __first, const _Tp* __last);  
  141.   void insert(iterator __position,  
  142.               const_iterator __first, const_iterator __last);  
  143. #endif /* __STL_MEMBER_TEMPLATES */  
  144.     
  145.   //在链表头插入节点  
  146.   void push_front(const _Tp& __x) { insert(begin(), __x); }  
  147.   void push_front() {insert(begin());}  
  148.   //在链表尾插入节点  
  149.   void push_back(const _Tp& __x) { insert(end(), __x); }  
  150.   void push_back() {insert(end());}  
  151.   
  152.   //***********************************************************  
  153.   //********************在指定位置删除节点*********************  
  154.   //********************以下是删除节点的公共接口***************  
  155.   /************************************************************  
  156.     //删除指定位置pos的节点  
  157.     iterator erase( iterator pos );  
  158.     iterator erase( const_iterator pos );  
  159.   
  160.     //删除指定范围[first,last)的数据节点  
  161.     iterator erase( iterator first, iterator last );  
  162.     iterator erase( const_iterator first, const_iterator last );  
  163.   ************************************************************/  
  164.   //***********************************************************  
  165.   //在指定位置position删除节点,并返回直接后继节点的地址  
  166.   iterator erase(iterator __position) {  
  167.       //调整前驱和后继节点的位置  
  168.     _List_node_base* __next_node = __position._M_node->_M_next;  
  169.     _List_node_base* __prev_node = __position._M_node->_M_prev;  
  170.     _Node* __n = (_Node*) __position._M_node;  
  171.     __prev_node->_M_next = __next_node;  
  172.     __next_node->_M_prev = __prev_node;  
  173.     _Destroy(&__n->_M_data);  
  174.     _M_put_node(__n);  
  175.     return iterator((_Node*) __next_node);  
  176.   }  
  177.   //删除两个迭代器之间的节点  
  178.   iterator erase(iterator __first, iterator __last);  
  179.   //清空链表,这里是调用父类的clear()函数  
  180.   void clear() { _Base::clear(); }  
  181.   
  182.   //调整链表的大小  
  183.   void resize(size_type __new_size, const _Tp& __x);  
  184.   void resize(size_type __new_size) { this->resize(__new_size, _Tp()); }  
  185.   
  186.   //取出第一个数据节点  
  187.   void pop_front() { erase(begin()); }  
  188.   //取出最后一个数据节点  
  189.   void pop_back() {   
  190.     iterator __tmp = end();  
  191.     erase(--__tmp);  
  192.   }  
  193.   
  194. public:  
  195.   // assign(), a generalized assignment member function.  Two  
  196.   // versions: one that takes a count, and one that takes a range.  
  197.   // The range version is a member template, so we dispatch on whether  
  198.   // or not the type is an integer.  
  199.   /*********************************************************************  
  200.   //assign()函数的两个版本原型,功能是在已定义的list容器填充值  
  201.     void assign( size_type count, const T& value );  
  202.   
  203.     templateclass InputIt >  
  204.     void assign( InputIt first, InputIt last );  
  205.   //*******************************************************************  
  206.     例子:  
  207.     #include <list>  
  208.     #include <iostream>  
  209.    
  210.     int main()  
  211.     {  
  212.         std::list<char> characters;  
  213.         //若定义characters时并初始化为字符b,下面的填充操作一样有效  
  214.         //std::list<char>characters(5,'b')  
  215.    
  216.         characters.assign(5, 'a');  
  217.    
  218.         for (char c : characters) {  
  219.             std::cout << c << ' ';  
  220.         }  
  221.    
  222.         return 0;  
  223.     }  
  224.     输出结果:a a a a a  
  225.   *********************************************************************/  
  226.     //这里是第一个版本void assign( size_type count, const T& value );  
  227.   void assign(size_type __n, const _Tp& __val) { _M_fill_assign(__n, __val); }  
  228.   
  229.   //这里为什么要把_M_fill_assign这个函数放在public呢??保护起来不是更好吗??  
  230.   void _M_fill_assign(size_type __n, const _Tp& __val);  
  231.   
  232. #ifdef __STL_MEMBER_TEMPLATES  
  233.   
  234.   //以下是针对assign()函数的第二个版本  
  235.   /* 
  236.     template< class InputIt > 
  237.     void assign( InputIt first, InputIt last ); 
  238.     这里有偏特化的现象,判断输入数据类型是否为整数型别 
  239.   */  
  240.   template <class _InputIterator>  
  241.   void assign(_InputIterator __first, _InputIterator __last) {  
  242.     typedef typename _Is_integer<_InputIterator>::_Integral _Integral;  
  243.     _M_assign_dispatch(__first, __last, _Integral());  
  244.   }  
  245.   
  246.   //若输入数据类型为整数型别,则派送到此函数  
  247.   template <class _Integer>  
  248.   void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)  
  249.     { _M_fill_assign((size_type) __n, (_Tp) __val); }  
  250.   
  251.   //若输入数据类型不是整数型别,则派送到此函数  
  252.   template <class _InputIterator>  
  253.   void _M_assign_dispatch(_InputIterator __first, _InputIterator __last,  
  254.                           __false_type);  
  255.   
  256. #endif /* __STL_MEMBER_TEMPLATES */  
  257.   //assign()函数定义结束  
  258.   //*****************************************************************  
  259.   
  260. protected:  
  261.     //把区间[first,last)的节点数据插入到指定节点position之前,position不能在区间内部  
  262.     //这个函数是list类的protected属性,不是公共接口,只为list类成员服务  
  263.     //为下面拼接函数void splice()服务  
  264.   void transfer(iterator __position, iterator __first, iterator __last) {  
  265.     if (__position != __last) {  
  266.       // Remove [first, last) from its old position.  
  267.       __last._M_node->_M_prev->_M_next     = __position._M_node;  
  268.       __first._M_node->_M_prev->_M_next    = __last._M_node;  
  269.       __position._M_node->_M_prev->_M_next = __first._M_node;   
  270.   
  271.       // Splice [first, last) into its new position.  
  272.       _List_node_base* __tmp      = __position._M_node->_M_prev;  
  273.       __position._M_node->_M_prev = __last._M_node->_M_prev;  
  274.       __last._M_node->_M_prev     = __first._M_node->_M_prev;   
  275.       __first._M_node->_M_prev    = __tmp;  
  276.     }  
  277.   }  
  278.   
  279. public:  
  280.     //**********************************************************  
  281.     //*******************拼接操作对外接口***********************  
  282.     //把链表拼接到当前链表指定位置position之前  
  283.     /*void splice(const_iterator pos, list& other);  
  284.       
  285.     //把it在链表other所指的位置拼接到当前链表pos之前,it和pos可指向同一链表  
  286.     void splice(const_iterator pos, list& other, const_iterator it);  
  287.   
  288.     //把链表other的节点范围[first,last)拼接在当前链表所指定的位置pos之前  
  289.     //[first,last)和pos可指向同一链表  
  290.     void splice(const_iterator pos, list& other,  
  291.             const_iterator first, const_iterator last);  
  292.     *************************************************************/  
  293.     //**********************************************************  
  294.     //将链表x拼接到当前链表的指定位置position之前  
  295.     //这里x和*this必须不同,即是两个不同的链表  
  296.   void splice(iterator __position, list& __x) {  
  297.     if (!__x.empty())   
  298.       this->transfer(__position, __x.begin(), __x.end());  
  299.   }  
  300.   //将i所指向的节点拼接到position所指位置之前  
  301.   //注意:i和position可以指向同一个链表  
  302.   void splice(iterator __position, list&, iterator __i) {  
  303.     iterator __j = __i;  
  304.     ++__j;  
  305.     //若i和position指向同一个链表,且指向同一位置  
  306.     //或者i和position指向同一个链表,且就在position的直接前驱位置  
  307.     //针对以上这两种情况,不做任何操作  
  308.     if (__position == __i || __position == __j) return;  
  309.     //否则,进行拼接操作  
  310.     this->transfer(__position, __i, __j);  
  311.   }  
  312.   //将范围[first,last)内所有节点拼接到position所指位置之前  
  313.   //注意:[first,last)和position可指向同一个链表,  
  314.   //但是position不能在[first,last)范围之内  
  315.   void splice(iterator __position, list&, iterator __first, iterator __last) {  
  316.     if (__first != __last)   
  317.       this->transfer(__position, __first, __last);  
  318.   }  
  319.   //以下是成员函数声明,定义在list类外实现  
  320.   //************************************************************  
  321.   //删除链表中值等于value的所有节点  
  322.   void remove(const _Tp& __value);  
  323.   //删除连续重复的元素节点,使之唯一  
  324.   //注意:是连续的重复元素  
  325.   void unique();  
  326.   //合并两个已排序的链表  
  327.   void merge(list& __x);  
  328.   //反转链表容器的内容  
  329.   void reverse();  
  330.   //按升序排序链表内容  
  331.   void sort();  
  332.   
  333. #ifdef __STL_MEMBER_TEMPLATES  
  334.   template <class _Predicate> void remove_if(_Predicate);  
  335.   template <class _BinaryPredicate> void unique(_BinaryPredicate);  
  336.   template <class _StrictWeakOrdering> void merge(list&, _StrictWeakOrdering);  
  337.   template <class _StrictWeakOrdering> void sort(_StrictWeakOrdering);  
  338. #endif /* __STL_MEMBER_TEMPLATES */  
  339. };  

细致介绍一个在pos位置插入一个节点的算法,和在数据结构中操作双向链表一样

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //*********************插入节点*****************************************  
  2.  /******************以下是插入节点函数的原型,也是公共接口**************  
  3. //在指定的位置pos之前插入值为value的数据节点  
  4. iterator insert( iterator pos, const T& value );  
  5. iterator insert( const_iterator pos, const T& value );  
  6.   
  7. //在指定的位置pos之前插入n个值为value的数据节点  
  8. void insert( iterator pos, size_type count, const T& value );  
  9. iterator insert( const_iterator pos, size_type count, const T& value );  
  10.   
  11. //在指定的位置pos之前插入[first,last)之间的数据节点  
  12. templateclass InputIt >  
  13. void insert( iterator pos, InputIt first, InputIt last);  
  14. templateclass InputIt >  
  15. iterator insert( const_iterator pos, InputIt first, InputIt last );  
  16.  ***********************************************************************/  
  17.  /**在整个链表的操作中,插入操作是非常重要的,很多成员函数会调用该函数**/  
  18. /***********************************************************************  
  19.  //在指定的位置插入初始值为x的节点  
  20.  iterator insert(iterator __position, const _Tp& __x) {  
  21.   //首先创建一个初始值为x的节点,并返回该节点的地址  
  22.    _Node* __tmp = _M_create_node(__x);  
  23. //调整节点指针,把新节点插入到指定位置  
  24.    __tmp->_M_next = __position._M_node;  
  25.    __tmp->_M_prev = __position._M_node->_M_prev;  
  26.    __position._M_node->_M_prev->_M_next = __tmp;  
  27.    __position._M_node->_M_prev = __tmp;  
  28. //返回新节点地址  
  29.    return __tmp;  
  30.  }  

0 0