STL源码—list transfer merge reverse

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

  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.   }  


merge函数


<span style="font-size:14px;">template<class T, class Alloc>
void list<T,Alloc>::merge(list<T,Alloc>&x)
{
	iterator first1 = begin();
	iterator last1 = end();
	iterator first2 = begin();
	iterator last2 = end();

	//<前提是两个序列都已经排序完成了
	while (first1 != last1 && first2!= last2)
	{
		if (*first2 < *first1)
		{
			iterator next = first2;
			transfer(first1,first2,++next);
			first2 = next;
		}
		else
			++first1;
		if (first2 != last2)
		{
			transfer(last1,first2,last2);
		}

	}
}</span>


reverse函数,还是借助transfer函数很容易就完成了翻转的工作

template<class T, class Alloc>
void list<T,Alloc>::reverse()
{
	if (node->next == node || link_type(node->next->next == node))
	{
		return;
	}
	iterator first = begin();
	++first;
	while (first != end())
	{
      iterator old = first;
	  ++first;
	  transfer(begin(),old,first);
	}
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值