rotate
若要在兩個相鄰範圍的項目。
template<class ForwardIterator>
void rotate(
ForwardIterator _First,
ForwardIterator _Middle,
ForwardIterator _Last
);
參數
_First
解決向前 Iterator 的第一個項目位置範圍旋轉。_Middle
定義在處理第一個項目位置範圍中的第二部分項目要切換與此範圍的第一部分的範圍內的正向 iterator 界限。_ Last
解決的 Iterator 向前到最後的項目位置的範圍旋轉。
備註
參考的範圍必須是有效的;所有指標必須 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++;
}
}
需求
標頭:<algorithm>
命名空間: std