partition/stable_partition详解

41 篇文章 0 订阅
39 篇文章 2 订阅

Partition:将满足条件的元素向前移动.

// TEMPLATE FUNCTION partition

template<class _BidIt,

class _Pr> inline

_BidIt _Partition(_BidIt _First, _BidIt _Last, _Pr _Pred)

{ // move elements satisfying _Pred to beginning of sequence

for (; ; ++_First)//最外层循环是一次往后面找元素

{ // find any out-of-order pair

for (; _First != _Last && _Pred(*_First); ++_First)//找到使得_Pred为false的元素

; // skip in-place elements at beginning

if (_First == _Last)

break; // done

for (; _First != --_Last && !_Pred(*_Last); )//找到是的_Pred为true的元素.

; // skip in-place elements at end

if (_First == _Last)

break; // done

_STD iter_swap(_First, _Last); // swap out-of-place pair and loop

//经过两个内循环的查找,前者是的_Pred为false,后者使得_Pred为true的元素已经找到,并交换彼此

}

return (_First);

}

需要注意的一点:vs2010stl实现方式中,很喜欢使用if( -- iter )/for(_First != --_Last)这类的表达式,这类表达式的有点是代码量少,程序整洁,但是缺点就是容易造成语义判断错误,比如--iter,即使if判断失败iter也减一操作.

Stable_partition:将满足条件的元素向前移动.(但不改变初始状态的相对顺序)

// TEMPLATE FUNCTION stable_partition

template<class _BidIt,

class _Pr,

class _Diff,

class _Ty> inline

_BidIt _Stable_partition(_BidIt _First, _BidIt _Last, _Pr _Pred,

_Diff _Count, _Temp_iterator<_Ty>& _Tempbuf)

{ // partition preserving order of equivalents, using _Pred

if (_Count == 0)

return (_First);

else if (_Count == 1)

return (_Pred(*_First) ? _Last : _First);

else if (_Count <= _Tempbuf._Maxlen())

{ // temp buffer big enough, copy right partition out and back

_BidIt _Next = _First;

for (_Tempbuf._Init(); _First != _Last; ++_First)

if (_Pred(*_First))

*_Next++ = _Move(*_First);

else

*_Tempbuf++ = _Move(*_First);

_Move(_Tempbuf._First(), _Tempbuf._Last(), _Next); // copy back

return (_Next);

}

else

{ // temp buffer not big enough, divide and conquer

_BidIt _Mid = _First;

_STD advance(_Mid, _Count / 2);

_BidIt _Left = _Stable_partition(_First, _Mid, _Pred,

_Count / 2, _Tempbuf); // form L1R1 in left half

_BidIt _Right = _Stable_partition(_Mid, _Last, _Pred,

_Count - _Count / 2, _Tempbuf); // form L2R2 in right half

_Diff _Count1 = 0;

_Distance(_Left, _Mid, _Count1);

_Diff _Count2 = 0;

_Distance(_Mid, _Right, _Count2);

return (_Buffered_rotate(_Left, _Mid, _Right,

_Count1, _Count2, _Tempbuf)); // rotate L1R1L2R2 to L1L2R1R2

}

}

这里涉及到另外一个类:_Temp_iterator,有涉及到_Move这个函数.

_Move没有看懂.想看看源码剖析,结果发现里面并没有找到这个函数的实现方式.再一次对源码剖析感到失望.

_Temp_iterator这个类不知道实际功能是做什么.

暂时就过这个函数.

举例:

int main()

{

vector<int> vecInt;

for ( int i = 1;i <= 9;++ i )

{

vecInt.push_back( i );

}

partition( vecInt.begin(),vecInt.end(),bind2nd( modulus<int>(),2 ) );

copy( vecInt.begin(),vecInt.end(),ostream_iterator<int>( cout," " ) );

cout<<"\nstable_partition:\n";

stable_partition( vecInt.begin(),vecInt.end(),bind2nd( modulus<int>(),2 ) );

copy( vecInt.begin(),vecInt.end(),ostream_iterator<int>( cout," " ) );

system( "pause" );

return 0;

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值