STL源码剖析——序列容器之list

前言

    在SGI STL中,list容器是一个循环的双向链表,它的内存空间效率较前文介绍的vector容器高。因为vector容器的内存空间是连续存储的,且在分配内存空间时,会分配额外的可用空间;而list容器的内存空间不一定是连续存储,内存之间是采用迭代器或节点指针进行连接,并且在插入或删除数据节点时,就配置或释放一个数据节点,并不会分配额外的内存空间,这两个操作过程都是常数时间。

    与vector容器不同的是list容器在进行插入操作或拼接操作时,迭代器并不会失效;且不能以普通指针作为迭代器,因为普通指针的+或-操作只能指向连续空间的后移地址或前移个地址,不能保证指向list的下一个节点,迭代器必须是双向迭代器,因为list容器具备有前移和后移的能力。

    注:本文所列的源码出自SGI STL中的文件<stl_list.h>,对于list容器类的详细信息也可以查看list容器库》MSDNlist类》

list容器

list节点和list数据结构

    在list容器中,list本身和list节点是分开设计的,list节点结构是存储数据和指向相邻节点的指针;如下源码所示:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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 容器是循环双向链表,则足够遍历整个链表;如下源码所示:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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. };  
     下面给出 list节点和list本身的数据结构图:


list容器的迭代器

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

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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 */  

list容器的构造函数和析构函数

    这里把list容器的构造函数列出来讲解,使我们对list容器的构造函数进行全面的了解,以便我们对其使用。在以下源码的前面我会总结出list容器的构造函数及其使用方法。

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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. };  

list容器的成员函数

    list容器的成员函数为我们使用该容器提供了很大的帮助,所以这里对其进行讲解,首先先给出源码的剖析,然后在对其中一些重点的成员函数进行图文讲解;具体源码剖析如下所示:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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. };  
     list 容器的成员函数中最重要的几个成员函数是插入 insert() 、擦除 erase() 、拼接 splice() 和排序 sort() 函数;以下利用图文的形式对其进行讲解;首先对插入节点函数进行insert()分析:

    下面的插入函数是在指定的位置插入初始值为value的节点,具体实现见下面源码剖析:

[cpp]  view plain  copy
  在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.  }  
            下面举一个例子对插入函数 insert() 进行图文分析:假设在以下 list 链表中节点5之前插入节点9,具体实现见下图步骤:注:图中的箭头旁边的数字表示语句的执行步骤

    第一步:首先初始化节点9,并为其分配节点空间;

    第二步:调整节点5指针方向,使其与节点9连接;

    第三步:调整节点5的前驱结点7指针的方向,使其与节点9连接;

    以下分析的是擦除指定位置的节点,详细见源码剖析:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //***********************************************************  
  2.  //********************在指定位置删除节点*********************  
  3.  //********************以下是删除节点的公共接口***************  
  4.  /************************************************************  
  5. //删除指定位置pos的节点  
  6. iterator erase( iterator pos );  
  7. iterator erase( const_iterator pos );  
  8.   
  9. //删除指定范围[first,last)的数据节点  
  10. iterator erase( iterator first, iterator last );  
  11. iterator erase( const_iterator first, const_iterator last );  
  12.  ************************************************************/  
  13.  //***********************************************************  
  14.  //在指定位置position删除节点,并返回直接后继节点的地址  
  15.  iterator erase(iterator __position) {  
  16.   //调整前驱和后继节点的位置  
  17.    _List_node_base* __next_node = __position._M_node->_M_next;  
  18.    _List_node_base* __prev_node = __position._M_node->_M_prev;  
  19.    _Node* __n = (_Node*) __position._M_node;  
  20.    __prev_node->_M_next = __next_node;  
  21.    __next_node->_M_prev = __prev_node;  
  22.    _Destroy(&__n->_M_data);  
  23.    _M_put_node(__n);  
  24.    return iterator((_Node*) __next_node);  
  25.  }  

    下面举一个例子对擦除函数erase()进行图文分析:假设在以下list链表中删除节点5,具体实现见下图步骤:图中的箭头旁边的数字表示语句的执行步骤

    第一步:首先调整待删除节点直接前驱指针,使其指向待删除节点的直接后继节点;

    第二步:调整待删除节点直接后继指针方向,使其指向待删除节点的直接前驱节点;

    第三步:释放待删除节点对象,回收待删除节点内存空;



     以下对迁移操作 transfer() 进行分析,该函数不是公共接口,属于list容器的保护成员函数,但是它为拼接函数服务,拼接函数的核心就是迁移函数; transfer() splice() 函数源代码剖析如下:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. protected:  
  2.     //把区间[first,last)的节点数据插入到指定节点position之前,position不能在区间内部  
  3.     //这个函数是list类的protected属性,不是公共接口,只为list类成员服务  
  4.     //为下面拼接函数void splice()服务  
  5.   void transfer(iterator __position, iterator __first, iterator __last) {  
  6.     if (__position != __last) {  
  7.       // Remove [first, last) from its old position.  
  8.       __last._M_node->_M_prev->_M_next     = __position._M_node;  
  9.       __first._M_node->_M_prev->_M_next    = __last._M_node;  
  10.       __position._M_node->_M_prev->_M_next = __first._M_node;   
  11.   
  12.       // Splice [first, last) into its new position.  
  13.       _List_node_base* __tmp      = __position._M_node->_M_prev;  
  14.       __position._M_node->_M_prev = __last._M_node->_M_prev;  
  15.       __last._M_node->_M_prev     = __first._M_node->_M_prev;   
  16.       __first._M_node->_M_prev    = __tmp;  
  17.     }  
  18.   }  
  19.   
  20. public:  
  21.     //**********************************************************  
  22.     //*******************拼接操作对外接口***********************  
  23.     //把链表拼接到当前链表指定位置position之前  
  24.     /*void splice(const_iterator pos, list& other);  
  25.       
  26.     //把it在链表other所指的位置拼接到当前链表pos之前,it和pos可指向同一链表  
  27.     void splice(const_iterator pos, list& other, const_iterator it);  
  28.   
  29.     //把链表other的节点范围[first,last)拼接在当前链表所指定的位置pos之前  
  30.     //[first,last)和pos可指向同一链表  
  31.     void splice(const_iterator pos, list& other,  
  32.             const_iterator first, const_iterator last);  
  33.     *************************************************************/  
  34.     //**********************************************************  
  35.     //将链表x拼接到当前链表的指定位置position之前  
  36.     //这里x和*this必须不同,即是两个不同的链表  
  37.   void splice(iterator __position, list& __x) {  
  38.     if (!__x.empty())   
  39.       this->transfer(__position, __x.begin(), __x.end());  
  40.   }  
  41.   //将i所指向的节点拼接到position所指位置之前  
  42.   //注意:i和position可以指向同一个链表  
  43.   void splice(iterator __position, list&, iterator __i) {  
  44.     iterator __j = __i;  
  45.     ++__j;  
  46.     //若i和position指向同一个链表,且指向同一位置  
  47.     //或者i和position指向同一个链表,且就在position的直接前驱位置  
  48.     //针对以上这两种情况,不做任何操作  
  49.     if (__position == __i || __position == __j) return;  
  50.     //否则,进行拼接操作  
  51.     this->transfer(__position, __i, __j);  
  52.   }  
  53.   //将范围[first,last)内所有节点拼接到position所指位置之前  
  54.   //注意:[first,last)和position可指向同一个链表,  
  55.   //但是position不能在[first,last)范围之内  
  56.   void splice(iterator __position, list&, iterator __first, iterator __last) {  
  57.     if (__first != __last)   
  58.       this->transfer(__position, __first, __last);  
  59.   }  
     下面用图文对该函数进行分析:注: transfer函数中的每一条语句按顺序对应图中执行步骤;

    下图是执行第一过程Remove[first, last) from its old position流图:


    下图是执行第二过程 Splice [first, last) into its new position流图:

    关于list容器的排序算法sort前面博文已经单独对其进行讲解,需要了解的请往前面博文STL源码剖析——list容器的排序算法sort()了解;

list容器的操作符重载

     关于操作符重载具体看源码剖析:
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //**************************************************************  
  2. //*****************以下是比较运算符操作符重载*******************  
  3. //**************************************************************  
  4. template <class _Tp, class _Alloc>  
  5. inline bool   
  6. operator==(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)  
  7. {  
  8.   typedef typename list<_Tp,_Alloc>::const_iterator const_iterator;  
  9.   const_iterator __end1 = __x.end();  
  10.   const_iterator __end2 = __y.end();  
  11.   
  12.   const_iterator __i1 = __x.begin();  
  13.   const_iterator __i2 = __y.begin();  
  14.   while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {  
  15.     ++__i1;  
  16.     ++__i2;  
  17.   }  
  18.   return __i1 == __end1 && __i2 == __end2;  
  19. }  
  20.   
  21. template <class _Tp, class _Alloc>  
  22. inline bool operator<(const list<_Tp,_Alloc>& __x,  
  23.                       const list<_Tp,_Alloc>& __y)  
  24. {  
  25.   return lexicographical_compare(__x.begin(), __x.end(),  
  26.                                  __y.begin(), __y.end());  
  27. }  
  28.   
  29. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER  
  30.   
  31. template <class _Tp, class _Alloc>  
  32. inline bool operator!=(const list<_Tp,_Alloc>& __x,  
  33.                        const list<_Tp,_Alloc>& __y) {  
  34.   return !(__x == __y);  
  35. }  
  36.   
  37. template <class _Tp, class _Alloc>  
  38. inline bool operator>(const list<_Tp,_Alloc>& __x,  
  39.                       const list<_Tp,_Alloc>& __y) {  
  40.   return __y < __x;  
  41. }  
  42.   
  43. template <class _Tp, class _Alloc>  
  44. inline bool operator<=(const list<_Tp,_Alloc>& __x,  
  45.                        const list<_Tp,_Alloc>& __y) {  
  46.   return !(__y < __x);  
  47. }  
  48.   
  49. template <class _Tp, class _Alloc>  
  50. inline bool operator>=(const list<_Tp,_Alloc>& __x,  
  51.                        const list<_Tp,_Alloc>& __y) {  
  52.   return !(__x < __y);  
  53. }  
  54.   
  55.   
  56. //交换两个链表内容  
  57. template <class _Tp, class _Alloc>  
  58. inline void   
  59. swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)  
  60. {  
  61.   __x.swap(__y);  
  62. }  
  63.   
  64. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */  
  65. //操作符重载结束  
  66. //**************************************************************  

list容器完整源码剖析

         list容器完成源码剖析:
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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. };  
  11.   
  12. //以下是链表List_iterator_base的迭代器  
  13. struct _List_iterator_base {  
  14.     //数据类型  
  15.   typedef size_t                     size_type;  
  16.   typedef ptrdiff_t                  difference_type;  
  17.   //list迭代器的类型是双向迭代器bidirectional_iterator  
  18.   typedef bidirectional_iterator_tag iterator_category;  
  19.   
  20.   //定义指向链表节点的指针  
  21.   _List_node_base* _M_node;  
  22.   
  23.   //构造函数  
  24.   _List_iterator_base(_List_node_base* __x) : _M_node(__x) {}  
  25.   _List_iterator_base() {}  
  26.   
  27.   //更新节点指针,指向直接前驱或直接后继节点  
  28.   void _M_incr() { _M_node = _M_node->_M_next; }  
  29.   void _M_decr() { _M_node = _M_node->_M_prev; }  
  30.   
  31.   //操作符重载  
  32.   bool operator==(const _List_iterator_base& __x) const {  
  33.     return _M_node == __x._M_node;  
  34.   }  
  35.   bool operator!=(const _List_iterator_base& __x) const {  
  36.     return _M_node != __x._M_node;  
  37.   }  
  38. };    
  39.   
  40. //以下是链表List_iterator的迭代器  
  41. template<class _Tp, class _Ref, class _Ptr>  
  42. struct _List_iterator : public _List_iterator_base {  
  43.   typedef _List_iterator<_Tp,_Tp&,_Tp*>             iterator;  
  44.   typedef _List_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;  
  45.   typedef _List_iterator<_Tp,_Ref,_Ptr>             _Self;  
  46.   
  47.   typedef _Tp value_type;  
  48.   typedef _Ptr pointer;  
  49.   typedef _Ref reference;  
  50.   typedef _List_node<_Tp> _Node;  
  51.   
  52.   //构造函数  
  53.   _List_iterator(_Node* __x) : _List_iterator_base(__x) {}  
  54.   _List_iterator() {}  
  55.   _List_iterator(const iterator& __x) : _List_iterator_base(__x._M_node) {}  
  56.   
  57.   //以下都是基本操作符的重载,取出节点数据  
  58.   reference operator*() const { return ((_Node*) _M_node)->_M_data; }  
  59.   
  60. #ifndef __SGI_STL_NO_ARROW_OPERATOR  
  61.   pointer operator->() const { return &(operator*()); }  
  62. #endif /* __SGI_STL_NO_ARROW_OPERATOR */  
  63.   
  64.   _Self& operator++() {   
  65.     this->_M_incr();  
  66.     return *this;  
  67.   }  
  68.   _Self operator++(int) {   
  69.     _Self __tmp = *this;  
  70.     this->_M_incr();  
  71.     return __tmp;  
  72.   }  
  73.   _Self& operator--() {   
  74.     this->_M_decr();  
  75.     return *this;  
  76.   }  
  77.   _Self operator--(int) {   
  78.     _Self __tmp = *this;  
  79.     this->_M_decr();  
  80.     return __tmp;  
  81.   }  
  82. };  
  83.   
  84. #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION  
  85.   
  86. //返回迭代器的类型  
  87. inline bidirectional_iterator_tag  
  88. iterator_category(const _List_iterator_base&)  
  89. {  
  90.   return bidirectional_iterator_tag();  
  91. }  
  92.   
  93. template <class _Tp, class _Ref, class _Ptr>  
  94. inline _Tp*  
  95. value_type(const _List_iterator<_Tp, _Ref, _Ptr>&)  
  96. {  
  97.   return 0;  
  98. }  
  99.   
  100. inline ptrdiff_t*  
  101. distance_type(const _List_iterator_base&)  
  102. {  
  103.   return 0;  
  104. }  
  105.   
  106. #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */  
  107.   
  108.   
  109. // Base class that encapsulates details of allocators.  Three cases:  
  110. // an ordinary standard-conforming allocator, a standard-conforming  
  111. // allocator with no non-static data, and an SGI-style allocator.  
  112. // This complexity is necessary only because we're worrying about backward  
  113. // compatibility and because we want to avoid wasting storage on an   
  114. // allocator instance if it isn't necessary.  
  115.   
  116. #ifdef __STL_USE_STD_ALLOCATORS  
  117.   
  118. // Base for general standard-conforming allocators.  
  119. template <class _Tp, class _Allocator, bool _IsStatic>  
  120. class _List_alloc_base {  
  121. public:  
  122.   typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type  
  123.           allocator_type;//返回节点配置器  
  124.   allocator_type get_allocator() const { return _Node_allocator; }  
  125.   
  126.   _List_alloc_base(const allocator_type& __a) : _Node_allocator(__a) {}  
  127.   
  128. protected:  
  129.   _List_node<_Tp>* _M_get_node()  
  130.    { return _Node_allocator.allocate(1); }  
  131.   void _M_put_node(_List_node<_Tp>* __p)  
  132.     { _Node_allocator.deallocate(__p, 1); }  
  133.   
  134. protected:  
  135.   typename _Alloc_traits<_List_node<_Tp>, _Allocator>::allocator_type  
  136.            _Node_allocator;  
  137.   _List_node<_Tp>* _M_node;  
  138. };  
  139.   
  140. // Specialization for instanceless allocators.  
  141. //instanceless分配器偏特化版  
  142. template <class _Tp, class _Allocator>  
  143. class _List_alloc_base<_Tp, _Allocator, true> {  
  144. public:  
  145.     //定义分配器类型  
  146.   typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type  
  147.           allocator_type;  
  148.   //返回节点配置器  
  149.   allocator_type get_allocator() const { return allocator_type(); }  
  150.   
  151.   //构造函数  
  152.   _List_alloc_base(const allocator_type&) {}  
  153.   
  154. protected:  
  155.   typedef typename _Alloc_traits<_List_node<_Tp>, _Allocator>::_Alloc_type  
  156.           _Alloc_type;  
  157.   //分配一个节点空间  
  158.   _List_node<_Tp>* _M_get_node() { return _Alloc_type::allocate(1); }  
  159.   //回收一个节点空间  
  160.   void _M_put_node(_List_node<_Tp>* __p) { _Alloc_type::deallocate(__p, 1); }  
  161.   
  162. protected:  
  163.     //定义节点指针  
  164.   _List_node<_Tp>* _M_node;  
  165. };  
  166.   
  167. template <class _Tp, class _Alloc>  
  168. class _List_base   
  169.   : public _List_alloc_base<_Tp, _Alloc,  
  170.                             _Alloc_traits<_Tp, _Alloc>::_S_instanceless>  
  171. {  
  172. public:  
  173.   typedef _List_alloc_base<_Tp, _Alloc,  
  174.                            _Alloc_traits<_Tp, _Alloc>::_S_instanceless>  
  175.           _Base;   
  176.   //allocator_type迭代器类型  
  177.   typedef typename _Base::allocator_type allocator_type;  
  178.   
  179.  //构造函数  
  180.   _List_base(const allocator_type& __a) : _Base(__a) {  
  181.     _M_node = _M_get_node();//分配一个节点空间  
  182.     _M_node->_M_next = _M_node;//  
  183.     _M_node->_M_prev = _M_node;  
  184.   }  
  185.   //析构函数  
  186.   ~_List_base() {  
  187.     clear();//清空链表  
  188.     _M_put_node(_M_node);//回收一个节点内存空间  
  189.   }  
  190.   
  191.   void clear();//清空链表  
  192. };  
  193.   
  194. #else /* __STL_USE_STD_ALLOCATORS */  
  195.   
  196. template <class _Tp, class _Alloc>  
  197. class _List_base   
  198. {  
  199. public:  
  200.   typedef _Alloc allocator_type;//获得分配器类型  
  201.   allocator_type get_allocator() const { return allocator_type(); }  
  202.   
  203.   //构造函数  
  204.   _List_base(const allocator_type&) {  
  205.     _M_node = _M_get_node();//分配一个节点空间  
  206.     //节点前驱和后继指针指向自己,表示是一个空链表  
  207.     _M_node->_M_next = _M_node;  
  208.     _M_node->_M_prev = _M_node;  
  209.   }  
  210.   //析构函数  
  211.   ~_List_base() {  
  212.     clear();//清空链表  
  213.     _M_put_node(_M_node);//回收一个节点内存空间  
  214.   }  
  215.   
  216.   void clear();//清空链表  
  217.   
  218. protected:  
  219.     //迭代器类型  
  220.   typedef simple_alloc<_List_node<_Tp>, _Alloc> _Alloc_type;  
  221.   //分配一个节点内存空间  
  222.   _List_node<_Tp>* _M_get_node() { return _Alloc_type::allocate(1); }  
  223.   //回收一个节点内存空间  
  224.   void _M_put_node(_List_node<_Tp>* __p) { _Alloc_type::deallocate(__p, 1); }   
  225.   
  226. protected:  
  227.   _List_node<_Tp>* _M_node;//链表的节点指针  
  228. };  
  229.   
  230. #endif /* __STL_USE_STD_ALLOCATORS */  
  231.   
  232. //clear()函数的实现,即清空链表  
  233. template <class _Tp, class _Alloc>  
  234. void   
  235. _List_base<_Tp,_Alloc>::clear()   
  236. {  
  237.  //选取_M_node->_M_next作为当前节点  
  238.  _List_node<_Tp>* __cur = (_List_node<_Tp>*) _M_node->_M_next;  
  239.   while (__cur != _M_node) {//遍历每一个节点  
  240.     _List_node<_Tp>* __tmp = __cur;//设置一个节点临时别名  
  241.     __cur = (_List_node<_Tp>*) __cur->_M_next;//指向下一个节点  
  242.     _Destroy(&__tmp->_M_data);//析构数据对象  
  243.     _M_put_node(__tmp);//回收节点tmp指向的内存空间  
  244.   }  
  245.   //空链表,即前驱和后继指针都指向自己  
  246.   _M_node->_M_next = _M_node;  
  247.   _M_node->_M_prev = _M_node;  
  248. }  
  249.   
  250. //以下是双向链表list类的定义,分配器_Alloc默认为第二级配置器  
  251. template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >  
  252. class list : protected _List_base<_Tp, _Alloc> {  
  253.   // requirements:  
  254.   
  255.   __STL_CLASS_REQUIRES(_Tp, _Assignable);  
  256.   
  257.   typedef _List_base<_Tp, _Alloc> _Base;  
  258. protected:  
  259.   typedef void* _Void_pointer;//定义指针类型  
  260.   
  261. public//以下是内嵌型别  
  262.   typedef _Tp value_type;  
  263.   typedef value_type* pointer;  
  264.   typedef const value_type* const_pointer;  
  265.   typedef value_type& reference;  
  266.   typedef const value_type& const_reference;  
  267.   typedef _List_node<_Tp> _Node;  
  268.   typedef size_t size_type;  
  269.   typedef ptrdiff_t difference_type;  
  270.   
  271.   typedef typename _Base::allocator_type allocator_type;//分配器类型  
  272.   allocator_type get_allocator() const { return _Base::get_allocator(); }  
  273.   
  274. public:  
  275.     //迭代器的类型  
  276.   typedef _List_iterator<_Tp,_Tp&,_Tp*>             iterator;  
  277.   typedef _List_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;  
  278.   
  279. #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION  
  280.   typedef reverse_iterator<const_iterator> const_reverse_iterator;  
  281.   typedef reverse_iterator<iterator>       reverse_iterator;  
  282. #else /* __STL_CLASS_PARTIAL_SPECIALIZATION */  
  283.   typedef reverse_bidirectional_iterator<const_iterator,value_type,  
  284.                                          const_reference,difference_type>  
  285.           const_reverse_iterator;  
  286.   typedef reverse_bidirectional_iterator<iterator,value_type,reference,  
  287.                                          difference_type>  
  288.           reverse_iterator;   
  289. #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */  
  290.   
  291. protected:  
  292. #ifdef __STL_HAS_NAMESPACES  
  293.   using _Base::_M_node;  
  294.   using _Base::_M_put_node;  
  295.   using _Base::_M_get_node;  
  296. #endif /* __STL_HAS_NAMESPACES */  
  297.   
  298. protected:  
  299.     //创建值为x的节点,并返回该节点的地址  
  300.   _Node* _M_create_node(const _Tp& __x)  
  301.   {  
  302.     _Node* __p = _M_get_node();//分配一个节点空间  
  303.     __STL_TRY {//把x值赋予指定的地址,即是data值  
  304.       _Construct(&__p->_M_data, __x);  
  305.     }  
  306.     __STL_UNWIND(_M_put_node(__p));  
  307.     return __p;//返回节点地址  
  308.   }  
  309.   
  310.   //创建默认值的节点  
  311.   _Node* _M_create_node()  
  312.   {  
  313.     _Node* __p = _M_get_node();  
  314.     __STL_TRY {  
  315.       _Construct(&__p->_M_data);  
  316.     }  
  317.     __STL_UNWIND(_M_put_node(__p));  
  318.     return __p;  
  319.   }  
  320.   
  321. public:  
  322.       
  323.   
  324.   //以下是迭代器的定义  
  325.   iterator begin()             { return (_Node*)(_M_node->_M_next); }  
  326.   const_iterator begin() const { return (_Node*)(_M_node->_M_next); }  
  327.   
  328.   iterator end()             { return _M_node; }  
  329.   const_iterator end() const { return _M_node; }  
  330.   
  331.   reverse_iterator rbegin()   
  332.     { return reverse_iterator(end()); }  
  333.   const_reverse_iterator rbegin() const   
  334.     { return const_reverse_iterator(end()); }  
  335.   
  336.   reverse_iterator rend()  
  337.     { return reverse_iterator(begin()); }  
  338.   const_reverse_iterator rend() const  
  339.     { return const_reverse_iterator(begin()); }  
  340.   
  341.   //判断链表是否为空链表  
  342.   bool empty() const { return _M_node->_M_next == _M_node; }  
  343.    
  344.   //返回链表的大小  
  345.   size_type size() const {  
  346.     size_type __result = 0;  
  347.     //返回两个迭代器之间的距离  
  348.     distance(begin(), end(), __result);  
  349.     //返回链表的元素个数  
  350.     return __result;  
  351.   }  
  352.   size_type max_size() const { return size_type(-1); }  
  353.   
  354.   //返回第一个节点数据的引用,reference相当于value_type&  
  355.   reference front() { return *begin(); }  
  356.   const_reference front() const { return *begin(); }  
  357.   //返回最后一个节点数据的引用  
  358.   reference back() { return *(--end()); }  
  359.   const_reference back() const { return *(--end()); }  
  360.   
  361.   //交换链表容器的内容  
  362.   void swap(list<_Tp, _Alloc>& __x) { __STD::swap(_M_node, __x._M_node); }  
  363.   
  364.  //**********************************************************************  
  365.  //*********************插入节点*****************************************  
  366.   /******************以下是插入节点函数的原型,也是公共接口**************  
  367.     //在指定的位置pos之前插入值为value的数据节点  
  368.     iterator insert( iterator pos, const T& value );  
  369.     iterator insert( const_iterator pos, const T& value );  
  370.   
  371.     //在指定的位置pos之前插入n个值为value的数据节点  
  372.     void insert( iterator pos, size_type count, const T& value );  
  373.     iterator insert( const_iterator pos, size_type count, const T& value );  
  374.   
  375.     //在指定的位置pos之前插入[first,last)之间的数据节点  
  376.     templateclass InputIt >  
  377.     void insert( iterator pos, InputIt first, InputIt last);  
  378.     templateclass InputIt >  
  379.     iterator insert( const_iterator pos, InputIt first, InputIt last );  
  380.   ***********************************************************************/  
  381.   /**在整个链表的操作中,插入操作是非常重要的,很多成员函数会调用该函数**/  
  382. //***********************************************************************  
  383.   //在指定的位置插入初始值为x的节点  
  384.   iterator insert(iterator __position, const _Tp& __x) {  
  385.       //首先创建一个初始值为x的节点,并返回该节点的地址  
  386.     _Node* __tmp = _M_create_node(__x);  
  387.     //调整节点指针,把新节点插入到指定位置  
  388.     __tmp->_M_next = __position._M_node;  
  389.     __tmp->_M_prev = __position._M_node->_M_prev;  
  390.     __position._M_node->_M_prev->_M_next = __tmp;  
  391.     __position._M_node->_M_prev = __tmp;  
  392.     //返回新节点地址  
  393.     return __tmp;  
  394.   }  
  395.   //在指定的位置插入为默认值的节点  
  396.   iterator insert(iterator __position) { return insert(__position, _Tp()); }  
  397.   
  398.   //在指定位置插入n个初始值为x的节点  
  399.   void insert(iterator __pos, size_type __n, const _Tp& __x)  
  400.     { _M_fill_insert(__pos, __n, __x); }  
  401.   void _M_fill_insert(iterator __pos, size_type __n, const _Tp& __x);   
  402.   
  403. #ifdef __STL_MEMBER_TEMPLATES  
  404.   // Check whether it's an integral type.  If so, it's not an iterator.  
  405.   //这里采用__type_traits技术  
  406.    
  407.    //在指定位置插入指定范围内的数据  
  408.   //首先判断输入迭代器类型_InputIterator是否为整数类型  
  409.   template <class _InputIterator>  
  410.   void insert(iterator __pos, _InputIterator __first, _InputIterator __last) {  
  411.     typedef typename _Is_integer<_InputIterator>::_Integral _Integral;  
  412.     _M_insert_dispatch(__pos, __first, __last, _Integral());  
  413.   }  
  414.   
  415.     
  416.   //若输入迭代器类型_InputIterator是为整数类型,调用此函数  
  417.   template<class _Integer>  
  418.   void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,  
  419.                           __true_type) {  
  420.     _M_fill_insert(__pos, (size_type) __n, (_Tp) __x);  
  421.   }  
  422.   
  423.   //若输入迭代器类型_InputIterator是不为整数类型,调用此函数  
  424.   template <class _InputIterator>  
  425.   void _M_insert_dispatch(iterator __pos,  
  426.                           _InputIterator __first, _InputIterator __last,  
  427.                           __false_type);  
  428.   
  429.  #else /* __STL_MEMBER_TEMPLATES */  
  430.   void insert(iterator __position, const _Tp* __first, const _Tp* __last);  
  431.   void insert(iterator __position,  
  432.               const_iterator __first, const_iterator __last);  
  433. #endif /* __STL_MEMBER_TEMPLATES */  
  434.     
  435.   //在链表头插入节点  
  436.   void push_front(const _Tp& __x) { insert(begin(), __x); }  
  437.   void push_front() {insert(begin());}  
  438.   //在链表尾插入节点  
  439.   void push_back(const _Tp& __x) { insert(end(), __x); }  
  440.   void push_back() {insert(end());}  
  441.   
  442.   //***********************************************************  
  443.   //********************在指定位置删除节点*********************  
  444.   //********************以下是删除节点的公共接口***************  
  445.   /************************************************************  
  446.     //删除指定位置pos的节点  
  447.     iterator erase( iterator pos );  
  448.     iterator erase( const_iterator pos );  
  449.   
  450.     //删除指定范围[first,last)的数据节点  
  451.     iterator erase( iterator first, iterator last );  
  452.     iterator erase( const_iterator first, const_iterator last );  
  453.   ************************************************************/  
  454.   //***********************************************************  
  455.   //在指定位置position删除节点,并返回直接后继节点的地址  
  456.   iterator erase(iterator __position) {  
  457.       //调整前驱和后继节点的位置  
  458.     _List_node_base* __next_node = __position._M_node->_M_next;  
  459.     _List_node_base* __prev_node = __position._M_node->_M_prev;  
  460.     _Node* __n = (_Node*) __position._M_node;  
  461.     __prev_node->_M_next = __next_node;  
  462.     __next_node->_M_prev = __prev_node;  
  463.     _Destroy(&__n->_M_data);  
  464.     _M_put_node(__n);  
  465.     return iterator((_Node*) __next_node);  
  466.   }  
  467.   //删除两个迭代器之间的节点  
  468.   iterator erase(iterator __first, iterator __last);  
  469.   //清空链表,这里是调用父类的clear()函数  
  470.   void clear() { _Base::clear(); }  
  471.   
  472.   //调整链表的大小  
  473.   void resize(size_type __new_size, const _Tp& __x);  
  474.   void resize(size_type __new_size) { this->resize(__new_size, _Tp()); }  
  475.   
  476.   //取出第一个数据节点  
  477.   void pop_front() { erase(begin()); }  
  478.   //取出最后一个数据节点  
  479.   void pop_back() {   
  480.     iterator __tmp = end();  
  481.     erase(--__tmp);  
  482.   }  
  483.   
  484.   //**********************************************************************  
  485.   /***********************以下是构造函数**********************************  
  486.   //*******************默认构造函数***************************************  
  487.     explicit list( const Allocator& alloc = Allocator() );  
  488.   //**********************具有初值和大小的构造函数************************  
  489.     explicit list( size_type count,  
  490.                const T& value = T(),  
  491.                const Allocator& alloc = Allocator());  
  492.          list( size_type count,  
  493.                const T& value,  
  494.                const Allocator& alloc = Allocator());  
  495.   //**************只有大小的构造函数**************************************  
  496.     explicit list( size_type count );  
  497.   //************某个范围的值为初始值的构造函数****************************  
  498.     templateclass InputIt >  
  499.     list( InputIt first, InputIt last,  
  500.       const Allocator& alloc = Allocator() );  
  501.  //************拷贝构造函数***********************************************  
  502.     list( const list& other );  
  503.   */  
  504.   //**********************************************************************  
  505.   //构造函数  
  506.   //链表的默认构造函数  
  507.   explicit list(const allocator_type& __a = allocator_type()) : _Base(__a) {}  
  508.   list(size_type __n, const _Tp& __value,  
  509.        const allocator_type& __a = allocator_type())  
  510.     : _Base(__a)  
  511.     { insert(begin(), __n, __value); }  
  512.   explicit list(size_type __n)  
  513.     : _Base(allocator_type())  
  514.     { insert(begin(), __n, _Tp()); }  
  515.   
  516. #ifdef __STL_MEMBER_TEMPLATES  
  517.   
  518.   // We don't need any dispatching tricks here, because insert does all of  
  519.   // that anyway.    
  520.   template <class _InputIterator>  
  521.   list(_InputIterator __first, _InputIterator __last,  
  522.        const allocator_type& __a = allocator_type())  
  523.     : _Base(__a)  
  524.     { insert(begin(), __first, __last); }  
  525.   
  526. #else /* __STL_MEMBER_TEMPLATES */  
  527.   
  528.   list(const _Tp* __first, const _Tp* __last,  
  529.        const allocator_type& __a = allocator_type())  
  530.     : _Base(__a)  
  531.     { this->insert(begin(), __first, __last); }  
  532.   list(const_iterator __first, const_iterator __last,  
  533.        const allocator_type& __a = allocator_type())  
  534.     : _Base(__a)  
  535.     { this->insert(begin(), __first, __last); }  
  536.   
  537. #endif /* __STL_MEMBER_TEMPLATES */  
  538.   list(const list<_Tp, _Alloc>& __x) : _Base(__x.get_allocator())  
  539.     { insert(begin(), __x.begin(), __x.end()); }//拷贝构造函数  
  540.   
  541.   ~list() { }//析构函数  
  542.   
  543.   //赋值操作  
  544.   list<_Tp, _Alloc>& operator=(const list<_Tp, _Alloc>& __x);  
  545.   //构造函数,析构函数,赋值操作 定义到此结束  
  546.   //*******************************************************************  
  547.   
  548. public:  
  549.   // assign(), a generalized assignment member function.  Two  
  550.   // versions: one that takes a count, and one that takes a range.  
  551.   // The range version is a member template, so we dispatch on whether  
  552.   // or not the type is an integer.  
  553.   /*********************************************************************  
  554.   //assign()函数的两个版本原型,功能是在已定义的list容器填充值  
  555.     void assign( size_type count, const T& value );  
  556.   
  557.     templateclass InputIt >  
  558.     void assign( InputIt first, InputIt last );  
  559.   //*******************************************************************  
  560.     例子:  
  561.     #include <list>  
  562.     #include <iostream>  
  563.    
  564.     int main()  
  565.     {  
  566.         std::list<char> characters;  
  567.         //若定义characters时并初始化为字符b,下面的填充操作一样有效  
  568.         //std::list<char>characters(5,'b')  
  569.    
  570.         characters.assign(5, 'a');  
  571.    
  572.         for (char c : characters) {  
  573.             std::cout << c << ' ';  
  574.         }  
  575.    
  576.         return 0;  
  577.     }  
  578.     输出结果:a a a a a  
  579.   *********************************************************************/  
  580.     //这里是第一个版本void assign( size_type count, const T& value );  
  581.   void assign(size_type __n, const _Tp& __val) { _M_fill_assign(__n, __val); }  
  582.   
  583.   //这里为什么要把_M_fill_assign这个函数放在public呢??保护起来不是更好吗??  
  584.   void _M_fill_assign(size_type __n, const _Tp& __val);  
  585.   
  586. #ifdef __STL_MEMBER_TEMPLATES  
  587.   
  588.   //以下是针对assign()函数的第二个版本  
  589.   /* 
  590.     template< class InputIt > 
  591.     void assign( InputIt first, InputIt last ); 
  592.     这里有偏特化的现象,判断输入数据类型是否为整数型别 
  593.   */  
  594.   template <class _InputIterator>  
  595.   void assign(_InputIterator __first, _InputIterator __last) {  
  596.     typedef typename _Is_integer<_InputIterator>::_Integral _Integral;  
  597.     _M_assign_dispatch(__first, __last, _Integral());  
  598.   }  
  599.   
  600.   //若输入数据类型为整数型别,则派送到此函数  
  601.   template <class _Integer>  
  602.   void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)  
  603.     { _M_fill_assign((size_type) __n, (_Tp) __val); }  
  604.   
  605.   //若输入数据类型不是整数型别,则派送到此函数  
  606.   template <class _InputIterator>  
  607.   void _M_assign_dispatch(_InputIterator __first, _InputIterator __last,  
  608.                           __false_type);  
  609.   
  610. #endif /* __STL_MEMBER_TEMPLATES */  
  611.   //assign()函数定义结束  
  612.   //*****************************************************************  
  613.   
  614. protected:  
  615.     //把区间[first,last)的节点数据插入到指定节点position之前,position不能在区间内部  
  616.     //这个函数是list类的protected属性,不是公共接口,只为list类成员服务  
  617.     //为下面拼接函数void splice()服务  
  618.   void transfer(iterator __position, iterator __first, iterator __last) {  
  619.     if (__position != __last) {  
  620.       // Remove [first, last) from its old position.  
  621.       __last._M_node->_M_prev->_M_next     = __position._M_node;  
  622.       __first._M_node->_M_prev->_M_next    = __last._M_node;  
  623.       __position._M_node->_M_prev->_M_next = __first._M_node;   
  624.   
  625.       // Splice [first, last) into its new position.  
  626.       _List_node_base* __tmp      = __position._M_node->_M_prev;  
  627.       __position._M_node->_M_prev = __last._M_node->_M_prev;  
  628.       __last._M_node->_M_prev     = __first._M_node->_M_prev;   
  629.       __first._M_node->_M_prev    = __tmp;  
  630.     }  
  631.   }  
  632.   
  633. public:  
  634.     //**********************************************************  
  635.     //*******************拼接操作对外接口***********************  
  636.     //把链表拼接到当前链表指定位置position之前  
  637.     /*void splice(const_iterator pos, list& other);  
  638.       
  639.     //把it在链表other所指的位置拼接到当前链表pos之前,it和pos可指向同一链表  
  640.     void splice(const_iterator pos, list& other, const_iterator it);  
  641.   
  642.     //把链表other的节点范围[first,last)拼接在当前链表所指定的位置pos之前  
  643.     //[first,last)和pos可指向同一链表  
  644.     void splice(const_iterator pos, list& other,  
  645.             const_iterator first, const_iterator last);  
  646.     *************************************************************/  
  647.     //**********************************************************  
  648.     //将链表x拼接到当前链表的指定位置position之前  
  649.     //这里x和*this必须不同,即是两个不同的链表  
  650.   void splice(iterator __position, list& __x) {  
  651.     if (!__x.empty())   
  652.       this->transfer(__position, __x.begin(), __x.end());  
  653.   }  
  654.   //将i所指向的节点拼接到position所指位置之前  
  655.   //注意:i和position可以指向同一个链表  
  656.   void splice(iterator __position, list&, iterator __i) {  
  657.     iterator __j = __i;  
  658.     ++__j;  
  659.     //若i和position指向同一个链表,且指向同一位置  
  660.     //或者i和position指向同一个链表,且就在position的直接前驱位置  
  661.     //针对以上这两种情况,不做任何操作  
  662.     if (__position == __i || __position == __j) return;  
  663.     //否则,进行拼接操作  
  664.     this->transfer(__position, __i, __j);  
  665.   }  
  666.   //将范围[first,last)内所有节点拼接到position所指位置之前  
  667.   //注意:[first,last)和position可指向同一个链表,  
  668.   //但是position不能在[first,last)范围之内  
  669.   void splice(iterator __position, list&, iterator __first, iterator __last) {  
  670.     if (__first != __last)   
  671.       this->transfer(__position, __first, __last);  
  672.   }  
  673.   //以下是成员函数声明,定义在list类外实现  
  674.   //************************************************************  
  675.   //删除链表中值等于value的所有节点  
  676.   void remove(const _Tp& __value);  
  677.   //删除连续重复的元素节点,使之唯一  
  678.   //注意:是连续的重复元素  
  679.   void unique();  
  680.   //合并两个已排序的链表  
  681.   void merge(list& __x);  
  682.   //反转链表容器的内容  
  683.   void reverse();  
  684.   //按升序排序链表内容  
  685.   void sort();  
  686.   
  687. #ifdef __STL_MEMBER_TEMPLATES  
  688.   template <class _Predicate> void remove_if(_Predicate);  
  689.   template <class _BinaryPredicate> void unique(_BinaryPredicate);  
  690.   template <class _StrictWeakOrdering> void merge(list&, _StrictWeakOrdering);  
  691.   template <class _StrictWeakOrdering> void sort(_StrictWeakOrdering);  
  692. #endif /* __STL_MEMBER_TEMPLATES */  
  693. };  
  694. //list定义结束  
  695. //**************************************************************  
  696.   
  697. //**************************************************************  
  698. //*****************以下是比较运算符操作符重载*******************  
  699. //**************************************************************  
  700. template <class _Tp, class _Alloc>  
  701. inline bool   
  702. operator==(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)  
  703. {  
  704.   typedef typename list<_Tp,_Alloc>::const_iterator const_iterator;  
  705.   const_iterator __end1 = __x.end();  
  706.   const_iterator __end2 = __y.end();  
  707.   
  708.   const_iterator __i1 = __x.begin();  
  709.   const_iterator __i2 = __y.begin();  
  710.   while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {  
  711.     ++__i1;  
  712.     ++__i2;  
  713.   }  
  714.   return __i1 == __end1 && __i2 == __end2;  
  715. }  
  716.   
  717. template <class _Tp, class _Alloc>  
  718. inline bool operator<(const list<_Tp,_Alloc>& __x,  
  719.                       const list<_Tp,_Alloc>& __y)  
  720. {  
  721.   return lexicographical_compare(__x.begin(), __x.end(),  
  722.                                  __y.begin(), __y.end());  
  723. }  
  724.   
  725. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER  
  726.   
  727. template <class _Tp, class _Alloc>  
  728. inline bool operator!=(const list<_Tp,_Alloc>& __x,  
  729.                        const list<_Tp,_Alloc>& __y) {  
  730.   return !(__x == __y);  
  731. }  
  732.   
  733. template <class _Tp, class _Alloc>  
  734. inline bool operator>(const list<_Tp,_Alloc>& __x,  
  735.                       const list<_Tp,_Alloc>& __y) {  
  736.   return __y < __x;  
  737. }  
  738.   
  739. template <class _Tp, class _Alloc>  
  740. inline bool operator<=(const list<_Tp,_Alloc>& __x,  
  741.                        const list<_Tp,_Alloc>& __y) {  
  742.   return !(__y < __x);  
  743. }  
  744.   
  745. template <class _Tp, class _Alloc>  
  746. inline bool operator>=(const list<_Tp,_Alloc>& __x,  
  747.                        const list<_Tp,_Alloc>& __y) {  
  748.   return !(__x < __y);  
  749. }  
  750.   
  751.   
  752. //交换两个链表内容  
  753. template <class _Tp, class _Alloc>  
  754. inline void   
  755. swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)  
  756. {  
  757.   __x.swap(__y);  
  758. }  
  759.   
  760. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */  
  761. //操作符重载结束  
  762. //**************************************************************  
  763.   
  764. //以下是list类成员函数的具体定义  
  765. //**************************************************************  
  766. #ifdef __STL_MEMBER_TEMPLATES  
  767.   
  768. template <class _Tp, class _Alloc> template <class _InputIter>  
  769. void   
  770. list<_Tp, _Alloc>::_M_insert_dispatch(iterator __position,  
  771.                                       _InputIter __first, _InputIter __last,  
  772.                                       __false_type)  
  773. {  
  774.   for ( ; __first != __last; ++__first)//遍历范围[first,last)  
  775.     insert(__position, *__first);//一个一个节点插入  
  776. }  
  777.   
  778. #else /* __STL_MEMBER_TEMPLATES */  
  779.   
  780. template <class _Tp, class _Alloc>  
  781. void   
  782. list<_Tp, _Alloc>::insert(iterator __position,   
  783.                           const _Tp* __first, const _Tp* __last)  
  784. {  
  785.   for ( ; __first != __last; ++__first)//遍历范围[first,last)  
  786.     insert(__position, *__first);//一个一个节点插入  
  787. }  
  788.   
  789. template <class _Tp, class _Alloc>  
  790. void   
  791. list<_Tp, _Alloc>::insert(iterator __position,  
  792.                          const_iterator __first, const_iterator __last)  
  793. {  
  794.   for ( ; __first != __last; ++__first)//遍历范围[first,last)  
  795.     insert(__position, *__first);//一个一个节点插入  
  796. }  
  797.   
  798. #endif /* __STL_MEMBER_TEMPLATES */  
  799.   
  800. template <class _Tp, class _Alloc>  
  801. void   
  802. list<_Tp, _Alloc>::_M_fill_insert(iterator __position,  
  803.                                   size_type __n, const _Tp& __x)  
  804. {  
  805.   for ( ; __n > 0; --__n)//插入n个节点  
  806.     insert(__position, __x);//在position之前插入x节点  
  807. }  
  808.   
  809. template <class _Tp, class _Alloc>  
  810. typename list<_Tp,_Alloc>::iterator list<_Tp, _Alloc>::erase(iterator __first,   
  811.                                                              iterator __last)  
  812. {  
  813.   while (__first != __last)//遍历范围[first,last)  
  814.     erase(__first++);//一个一个节点删除  
  815.   return __last;  
  816. }  
  817.   
  818. //重新调整容器的大小  
  819. template <class _Tp, class _Alloc>  
  820. void list<_Tp, _Alloc>::resize(size_type __new_size, const _Tp& __x)  
  821. {  
  822.   iterator __i = begin();  
  823.   size_type __len = 0;//表示容器的原始大小  
  824.   for ( ; __i != end() && __len < __new_size; ++__i, ++__len)  
  825.     ;  
  826.   if (__len == __new_size)//若容器新的长度比原来的小,则擦除多余的元素  
  827.     erase(__i, end());  
  828.   else//若容器新的长度比原来的大,则把其初始化为x值   // __i == end()  
  829.     insert(end(), __new_size - __len, __x);  
  830. }  
  831.   
  832. //赋值操作  
  833. template <class _Tp, class _Alloc>  
  834. list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(const list<_Tp, _Alloc>& __x)  
  835. {  
  836.   if (this != &__x) {  
  837.     iterator __first1 = begin();  
  838.     iterator __last1 = end();  
  839.     const_iterator __first2 = __x.begin();  
  840.     const_iterator __last2 = __x.end();  
  841.     while (__first1 != __last1 && __first2 != __last2)   
  842.       *__first1++ = *__first2++;  
  843.     if (__first2 == __last2)//若当前容器的大小大于x容器大小  
  844.       erase(__first1, __last1);//则擦除多余部分  
  845.     else//若当前容器大小小于x容器大小,则把x容器剩下的数据插入到当前容器尾  
  846.       insert(__last1, __first2, __last2);  
  847.     //上面if语句里面的语句可以用下面代替  
  848.     /* 
  849.         clear(); 
  850.         this->assign(__x.begin(),__x.end()); 
  851.     */  
  852.   }  
  853.   return *this;  
  854. }  
  855.   
  856. //在已定义list容器中填充n个初始值为val的节点  
  857. template <class _Tp, class _Alloc>  
  858. void list<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {  
  859.   iterator __i = begin();  
  860.   for ( ; __i != end() && __n > 0; ++__i, --__n)  
  861.     *__i = __val;  
  862.   if (__n > 0)//若容器大小不够存储n个节点,则使用插入函数  
  863.     insert(end(), __n, __val);  
  864.   else//若容器原来的数据大小比n大,则擦除多余的数据  
  865.     erase(__i, end());  
  866.   //注:个人认为该函数也可以这样实现:  
  867.   //首先清空容器原来的内容  
  868.   //然后在容器插入n个值为val的数据节点  
  869.   /* 
  870.   _Tp tmp = __val; 
  871.   clear(); 
  872.   insert(begin(),__n,__val); 
  873.   */  
  874. }  
  875.   
  876. #ifdef __STL_MEMBER_TEMPLATES  
  877.   
  878. //若输入数据类型不是整数型别时,assign(_InputIter __first, _InputIter __last)调用该函数  
  879. //在[first,last)实现填充数值操作  
  880. template <class _Tp, class _Alloc> template <class _InputIter>  
  881. void  
  882. list<_Tp, _Alloc>::_M_assign_dispatch(_InputIter __first2, _InputIter __last2,  
  883.                                       __false_type)  
  884. {  
  885.   //获取原始容器的大小  
  886.   iterator __first1 = begin();  
  887.   iterator __last1 = end();  
  888.   //若原始容器和[first2,last2)大小不为0或1,则进行赋值操作  
  889.   for ( ; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2)  
  890.     *__first1 = *__first2;  
  891.   if (__first2 == __last2)//若原始容器的大小比[first2,last2)大  
  892.     erase(__first1, __last1);  
  893.   else  
  894.       //若原始容器的大小比[first2,last2)小  
  895.     insert(__last1, __first2, __last2);  
  896. }  
  897.   
  898. #endif /* __STL_MEMBER_TEMPLATES */  
  899.   
  900. //删除容器中值为value的所有数据节点  
  901. template <class _Tp, class _Alloc>  
  902. void list<_Tp, _Alloc>::remove(const _Tp& __value)  
  903. {  
  904.   iterator __first = begin();  
  905.   iterator __last = end();  
  906.   while (__first != __last) {//遍历整个容器  
  907.     iterator __next = __first;  
  908.     ++__next;  
  909.     if (*__first == __value) erase(__first);//若存在该值,则擦除  
  910.     __first = __next;//继续查找,直到first == last  
  911.   }  
  912. }  
  913.   
  914. //  
  915. template <class _Tp, class _Alloc>  
  916. void list<_Tp, _Alloc>::unique()  
  917. {  
  918.   iterator __first = begin();  
  919.   iterator __last = end();  
  920.   if (__first == __last) return;//若为空容器,则退出  
  921.   iterator __next = __first;  
  922.   while (++__next != __last) {//若容器大小大于1,进入while循环  
  923.     if (*__first == *__next)//若相邻元素相同  
  924.       erase(__next);//则擦除  
  925.     else//否则,查找下一节点  
  926.       __first = __next;  
  927.     __next = __first;  
  928.   }  
  929. }  
  930.   
  931. //合并两个已排序的链表,合并后的链表仍然是有序的  
  932. template <class _Tp, class _Alloc>  
  933. void list<_Tp, _Alloc>::merge(list<_Tp, _Alloc>& __x)  
  934. {  
  935.   iterator __first1 = begin();  
  936.   iterator __last1 = end();  
  937.   iterator __first2 = __x.begin();  
  938.   iterator __last2 = __x.end();  
  939.   while (__first1 != __last1 && __first2 != __last2)  
  940.     if (*__first2 < *__first1) {  
  941.       iterator __next = __first2;  
  942.       transfer(__first1, __first2, ++__next);//把first2拼接在first1之前  
  943.       __first2 = __next;  
  944.     }  
  945.     else  
  946.       ++__first1;  
  947.   //若链表x比当前链表长,则把剩余的数据节点拼接到当前链表的尾端  
  948.   if (__first2 != __last2) transfer(__last1, __first2, __last2);  
  949. }  
  950.   
  951.   
  952. inline void __List_base_reverse(_List_node_base* __p)  
  953. {  
  954.   _List_node_base* __tmp = __p;  
  955.   do {  
  956.     __STD::swap(__tmp->_M_next, __tmp->_M_prev);//交换指针所指的节点地址  
  957.     __tmp = __tmp->_M_prev;     // Old next node is now prev.  
  958.   } while (__tmp != __p);  
  959. }  
  960.   
  961. //把当前链表逆序  
  962. template <class _Tp, class _Alloc>  
  963. inline void list<_Tp, _Alloc>::reverse()   
  964. {  
  965.   __List_base_reverse(this->_M_node);  
  966. }      
  967.   
  968. //按升序进行排序,list链表的迭代器访问时双向迭代器  
  969. //因为STL的排序算法函数sort()是接受随机访问迭代器,在这里并不适合  
  970. template <class _Tp, class _Alloc>  
  971. void list<_Tp, _Alloc>::sort()  
  972. {  
  973.   // Do nothing if the list has length 0 or 1.  
  974.   if (_M_node->_M_next != _M_node && _M_node->_M_next->_M_next != _M_node)   
  975.   {  
  976.     list<_Tp, _Alloc> __carry;//carry链表起到搬运的作用  
  977.     //counter链表是中间存储作用  
  978.     /* 
  979.     *其中对于counter[i]里面最多的存储数据为2^(i+1)个节点 
  980.     *若超出则向高位进位即counter[i+1] 
  981.     */  
  982.     list<_Tp, _Alloc> __counter[64];  
  983.     int __fill = 0;  
  984.     while (!empty())   
  985.     {//若不是空链表  
  986.         //第一步:  
  987.       __carry.splice(__carry.begin(), *this, begin());//把当前链表的第一个节点放在carry链表头  
  988.       int __i = 0;  
  989.       while(__i < __fill && !__counter[__i].empty())   
  990.       {  
  991.         //第二步:  
  992.           __counter[__i].merge(__carry);//把链表carry合并到counter[i]  
  993.         //第三步:  
  994.           __carry.swap(__counter[__i++]);//交换链表carry和counter[i]内容  
  995.       }  
  996.       //第四步:  
  997.       __carry.swap(__counter[__i]);//交换链表carry和counter[i]内容           
  998.       //第五步:  
  999.       if (__i == __fill) ++__fill;  
  1000.     }   
  1001.   
  1002.     for (int __i = 1; __i < __fill; ++__i)  
  1003.       //第六步:  
  1004.         __counter[__i].merge(__counter[__i-1]);//把低位不满足进位的剩余数据全部有序的合并到上一位  
  1005.     //第七步:  
  1006.     swap(__counter[__fill-1]);//最后把已排序好的链表内容交换到当前链表  
  1007.   }  
  1008. }  
  1009.   
  1010. #ifdef __STL_MEMBER_TEMPLATES  
  1011.   
  1012. template <class _Tp, class _Alloc> template <class _Predicate>  
  1013. void list<_Tp, _Alloc>::remove_if(_Predicate __pred)  
  1014. {  
  1015.   iterator __first = begin();  
  1016.   iterator __last = end();  
  1017.   while (__first != __last) {  
  1018.     iterator __next = __first;  
  1019.     ++__next;  
  1020.     if (__pred(*__first)) erase(__first);  
  1021.     __first = __next;  
  1022.   }  
  1023. }  
  1024.   
  1025. template <class _Tp, class _Alloc> template <class _BinaryPredicate>  
  1026. void list<_Tp, _Alloc>::unique(_BinaryPredicate __binary_pred)  
  1027. {  
  1028.   iterator __first = begin();  
  1029.   iterator __last = end();  
  1030.   if (__first == __last) return;  
  1031.   iterator __next = __first;  
  1032.   while (++__next != __last) {  
  1033.     if (__binary_pred(*__first, *__next))  
  1034.       erase(__next);  
  1035.     else  
  1036.       __first = __next;  
  1037.     __next = __first;  
  1038.   }  
  1039. }  
  1040.   
  1041. template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>  
  1042. void list<_Tp, _Alloc>::merge(list<_Tp, _Alloc>& __x,  
  1043.                               _StrictWeakOrdering __comp)  
  1044. {  
  1045.   iterator __first1 = begin();  
  1046.   iterator __last1 = end();  
  1047.   iterator __first2 = __x.begin();  
  1048.   iterator __last2 = __x.end();  
  1049.   while (__first1 != __last1 && __first2 != __last2)  
  1050.     if (__comp(*__first2, *__first1)) {  
  1051.       iterator __next = __first2;  
  1052.       transfer(__first1, __first2, ++__next);  
  1053.       __first2 = __next;  
  1054.     }  
  1055.     else  
  1056.       ++__first1;  
  1057.   if (__first2 != __last2) transfer(__last1, __first2, __last2);  
  1058. }  
  1059.   
  1060. template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>  
  1061. void list<_Tp, _Alloc>::sort(_StrictWeakOrdering __comp)  
  1062. {  
  1063.   // Do nothing if the list has length 0 or 1.  
  1064.   if (_M_node->_M_next != _M_node && _M_node->_M_next->_M_next != _M_node) {  
  1065.     list<_Tp, _Alloc> __carry;  
  1066.     list<_Tp, _Alloc> __counter[64];  
  1067.     int __fill = 0;  
  1068.     while (!empty()) {  
  1069.       __carry.splice(__carry.begin(), *this, begin());  
  1070.       int __i = 0;  
  1071.       while(__i < __fill && !__counter[__i].empty()) {  
  1072.         __counter[__i].merge(__carry, __comp);  
  1073.         __carry.swap(__counter[__i++]);  
  1074.       }  
  1075.       __carry.swap(__counter[__i]);           
  1076.       if (__i == __fill) ++__fill;  
  1077.     }   
  1078.   
  1079.     for (int __i = 1; __i < __fill; ++__i)   
  1080.       __counter[__i].merge(__counter[__i-1], __comp);  
  1081.     swap(__counter[__fill-1]);  
  1082.   }  
  1083. }  
  1084.   
  1085. #endif /* __STL_MEMBER_TEMPLATES */  
  1086.   
  1087. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)  
  1088. #pragma reset woff 1174  
  1089. #pragma reset woff 1375  
  1090. #endif  
  1091.   
  1092. __STL_END_NAMESPACE   
  1093.   
  1094. #endif /* __SGI_STL_INTERNAL_LIST_H */  
  1095.   
  1096. // Local Variables:  
  1097. // mode:C++  
  1098. // End:  
     参考资料:
      《STL源码剖析》侯捷
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值