rotate
交换是在两个相邻范围的元素。
template<class ForwardIterator>
void rotate(
ForwardIterator _First,
ForwardIterator _Middle,
ForwardIterator _Last
);
参数
_First
处理第一元素的位置将向前迭代器在中旋转的范围。_Middle
定义在处理第一个元素在位置的范围的第二部分元素都将交换使用这些在范围的第一部分的范围中向前迭代器的边界。_ Last
寻址最终元素的前向迭代器位置一将旋转的范围。
备注
引用的范围必须是有效的;所有指针必须 dereferenceable,然后在序列中的最后位置从开始来访问通过递增。
复杂是线性的。最 (_Last - _First) 交换。
示例
// alg_rotate.cpp
// compile with: /EHsc
#include <vector>
#include <deque>
#include <algorithm>
#include <iostream>
int main( ) {
using namespace std;
vector <int> v1;
deque <int> d1;
vector <int>::iterator v1Iter1;
deque<int>::iterator d1Iter1;
int i;
for ( i = -3 ; i <= 5 ; i++ )
{
v1.push_back( i );
}
int ii;
for ( ii =0 ; ii <= 5 ; ii++ )
{
d1.push_back( ii );
}
cout << "Vector v1 is ( " ;
for ( v1Iter1 = v1.begin( ) ; v1Iter1 != v1.end( ) ;v1Iter1 ++ )
cout << *v1Iter1 << " ";
cout << ")." << endl;
rotate ( v1.begin ( ) , v1.begin ( ) + 3 , v1.end ( ) );
cout << "After rotating, vector v1 is ( " ;
for ( v1Iter1 = v1.begin( ) ; v1Iter1 != v1.end( ) ;v1Iter1 ++ )
cout << *v1Iter1 << " ";
cout << ")." << endl;
cout << "The original deque d1 is ( " ;
for ( d1Iter1 = d1.begin( ) ; d1Iter1 != d1.end( ) ;d1Iter1 ++ )
cout << *d1Iter1 << " ";
cout << ")." << endl;
int iii = 1;
while ( iii <= d1.end ( ) - d1.begin ( ) ) {
rotate ( d1.begin ( ) , d1.begin ( ) + 1 , d1.end ( ) );
cout << "After the rotation of a single deque element to the back,\n d1 is ( " ;
for ( d1Iter1 = d1.begin( ) ; d1Iter1 != d1.end( ) ;d1Iter1 ++ )
cout << *d1Iter1 << " ";
cout << ")." << endl;
iii++;
}
}
要求
标头: <算法>
命名空间: std