rotate/rotate_copy详解
来源:互联网 发布:php __get 编辑:程序博客网 时间:2024/06/08 08:18
旋转元素次序:rotate
源码:
template<class _RanIt,
class _Diff,
class _Ty> inline
void _Rotate(_RanIt _First, _RanIt _Mid, _RanIt _Last, _Diff *, _Ty *)
{ // rotate [_First, _Last), random-access iterators
_Diff _Shift = _Mid - _First;
_Diff _Count = _Last - _First;
for (_Diff _Factor = _Shift; _Factor != 0; )//求最大公因子
{ // find subcycle count as GCD of shift count and length
_Diff _Tmp = _Count % _Factor;
_Count = _Factor;
_Factor = _Tmp;
}
if (_Count < _Last - _First)
for (; 0 < _Count; --_Count)
{ // rotate each subcycle
_RanIt _Hole = _First + _Count;
_RanIt _Next = _Hole;
_RanIt _Next1 = _Next + _Shift == _Last ? _First : _Next + _Shift;
for (; ; )
{ // percolate elements back around subcycle
iter_swap(_Next, _Next1);
_Next = _Next1;
_Next1 = _Shift < _Last - _Next1 ? _Next1 + _Shift
: _First + (_Shift - (_Last - _Next1));
if (_Next1 == _Hole)
break;
}
}
}
int main()
{
vector<int> vecInt;
for( int i = 0;i < 10;++ i )
{
vecInt.push_back( i * 10 );
}
rotate( vecInt.begin(),vecInt.begin() + 7,vecInt.end() );
copy( vecInt.begin(),vecInt.end(),ostream_iterator<int>( cout," " ) );
system( "pause" );
return 0;
}
这段代码确实难懂,看了几遍没有看懂.
想去借助源码剖析,结果听失望的,侯捷对于forward和bidirectional iterator两个版本有细致的讲解,唯独random access iterator没有.
前面两个算法易懂,关键是后一个算法.__rotate_cycle在干嘛呢?这样做有的原理是啥呢?还是不清楚.感觉侯捷可能自己也没法讲解,"故意"跳过.
关于这个函数的讲解可能就暂时过了.去Google一下,发现在博客园里面有一篇文章对后面的那个函数解释的比较详细.但是它说到数论,这个没有学过.很可惜.
暂时先过吧.
- rotate/rotate_copy详解
- reverse,reverse_copy,rotate,rotate_copy
- STL 之reverse,reverse_copy,rotate,rotate_copy
- STL algorithm算法rotate,rotate_copy(51)
- C++标准库---旋转元素rotate()&rotate_copy()
- STL 之reverse,reverse_copy,rotate,rotate_copy
- STL_算法_旋转(rotate、rotate_copy)
- STL常用算法: fill,rotate,rotate_copy.
- rotate_copy
- STL_算法(25)_逆转和旋转 revers() 、reverse_copy()、rotate()、rotate_copy()、
- Collections Rotate详解
- sphinx –rotate机制详解
- canvas rotate中心点问题详解
- sphinx –rotate机制详解
- sphinx –rotate机制详解
- rotate
- Rotate
- Rotate
- 进程关系
- 第八天:servlet_httpRequest
- 第九天:cookie原理
- next_permutation/prev_permutation详解
- IOS设计UI工具大全
- rotate/rotate_copy详解
- xproto xextproto xtrans xcb >= 1.1.92 kbproto inputproto for android done.
- 移动应用设计中应避免的十种错误
- 程序人生--一个程序员对学弟学妹建议
- random_shuffle
- PHP5学习笔记-变量
- 分享8年开发经验,浅谈个人发展经历,明确自己发展方向
- SAP破解 Maintenance Certificate
- partition/stable_partition详解