C++算法rotate()函數用於旋轉範圍[first, last)內元素的順序。
- 序列將從源序列中間的元素開始,最後一個元素將在第一個元素之後。
- 中間到中間和最後一個元素之間的元素。
用法
template <class ForwardIterator>
void rotate (ForwardIterator first, ForwardIterator middle,
ForwardIterator last); // until C++ 11
template <class ForwardIterator>
ForwardIterator rotate (ForwardIterator first, ForwardIterator middle,
ForwardIterator last); //since C++ 11
參數
first:一個前向迭代器,指向要旋轉的範圍內第一個元素的位置。
middle: 向前迭代器尋址範圍 [first, last) 中移動到範圍中第一個位置的元素。
last:一個向前迭代器,指向元素被反轉的範圍內最後一個元素的位置。
返回值
空
複雜度
複雜度在 [first, last) 範圍內是線性的:交換或移動元素,直到所有元素都被重新定位。
數據競爭
範圍 [first, last) 中的對象被修改。
異常
如果元素交換或移動或迭代器上的操作引發異常,則此函數將引發異常。
請注意,無效參數會導致未定義的行為。
例子1
讓我們看看旋轉給定字符串的簡單示例:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello";
cout << "Before Rotate:"<< str << endl;
rotate(str.begin(), str.begin() + 2, str.end());
cout <<"After Rotate :" << str << endl;
return 0;
}
輸出:
Before Rotate:Hello After Rotate :lloHe
例子2
讓我們看另一個簡單的例子:
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;
void print(char a[], int N)
{
for(int i = 0; i < N; i++)
{
cout << (i + 1) << ". " << setw(2)
<< left << a[i] << " ";
}
cout << endl;
}
int main()
{
char s[] = {'A', 'B', 'C', 'D', 'E', 'G', 'H'};
int slen = sizeof(s) / sizeof(char);
cout << "Original order:";
print(s, slen);
cout << "Rotate with \'C\' as middle element" << endl;
rotate(s, s + 2, s + slen);
cout << "Rotated order :";
print(s, slen);
cout << "Rotate with \'G\' as middle element" << endl;
rotate(s, s + 3, s + slen);
cout << "Rotated order :";
print(s, slen);
cout << "Rotate with \'A\' as middle element" << endl;
rotate(s, s + 3, s + slen);
cout << "Original order:";
print(s, slen);
return 0;
}
輸出:
Original order:1. A 2. B 3. C 4. D 5. E 6. G 7. H Rotate with 'C' as middle element Rotated order :1. C 2. D 3. E 4. G 5. H 6. A 7. B Rotate with 'G' as middle element Rotated order :1. G 2. H 3. A 4. B 5. C 6. D 7. E Rotate with 'A' as middle element Original order:1. B 2. C 3. D 4. E 5. G 6. H 7. A
例子3
讓我們看另一個簡單的例子:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main () {
vector<int> vec1{1,2,3,4,5,6,7,8,9};
// Print old vector
cout << "Old vector:";
for(int i=0; i < vec1.size(); i++)
cout << " " << vec1[i];
cout << "\n";
// Rotate vector left 3 times.
int rotL=3;
// rotate function
rotate(vec1.begin(), vec1.begin()+rotL, vec1.end());
// Print new vector
cout << "New vector after left rotation:";
for (int i=0; i < vec1.size(); i++)
cout<<" "<<vec1[i];
cout << "\n\n";
vector <int> vec2{1,2,3,4,5,6,7,8,9};
// Print old vector
cout << "Old vector:";
for (int i=0; i < vec2.size(); i++)
cout << " " << vec2[i];
cout << "\n";
// Rotate vector right 4 times.
int rotR = 4;
// std::rotate function
rotate(vec2.begin(), vec2.begin()+vec2.size()-rotR, vec2.end());
// Print new vector
cout << "New vector after right rotation:";
for (int i=0; i < vec2.size(); i++)
cout << " " << vec2[i];
cout << "\n";
return 0;
}
輸出:
Old vector:1 2 3 4 5 6 7 8 9 New vector after left rotation:4 5 6 7 8 9 1 2 3 Old vector:1 2 3 4 5 6 7 8 9 New vector after right rotation:6 7 8 9 1 2 3 4 5
示例 4
讓我們看另一個簡單的例子:
#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++;
}
}
輸出:
Vector v1 is ( -3 -2 -1 0 1 2 3 4 5 ). After rotating, vector v1 is ( 0 1 2 3 4 5 -3 -2 -1 ). The original deque d1 is ( 0 1 2 3 4 5 ). After the rotation of a single deque element to the back, d1 is ( 1 2 3 4 5 0 ). After the rotation of a single deque element to the back, d1 is ( 2 3 4 5 0 1 ). After the rotation of a single deque element to the back, d1 is ( 3 4 5 0 1 2 ). After the rotation of a single deque element to the back, d1 is ( 4 5 0 1 2 3 ). After the rotation of a single deque element to the back, d1 is ( 5 0 1 2 3 4 ). After the rotation of a single deque element to the back, d1 is ( 0 1 2 3 4 5 ).
相關用法
- C++ Algorithm rotate_copy()用法及代碼示例
- C++ Algorithm remove_if()用法及代碼示例
- C++ Algorithm remove()用法及代碼示例
- C++ Algorithm remove_copy_if()用法及代碼示例
- C++ Algorithm random_shuffle()用法及代碼示例
- C++ Algorithm replace()用法及代碼示例
- C++ Algorithm reverse_copy()用法及代碼示例
- C++ Algorithm replace_copy()用法及代碼示例
- C++ Algorithm replace_copy_if()用法及代碼示例
- C++ Algorithm remove_copy()用法及代碼示例
- C++ Algorithm reverse()用法及代碼示例
- C++ Algorithm replace_if()用法及代碼示例
- C++ Algorithm max_element()用法及代碼示例
- C++ Algorithm set_union()用法及代碼示例
- C++ Algorithm next_permutation()用法及代碼示例
- C++ Algorithm upper_bound()用法及代碼示例
- C++ Algorithm minmax()用法及代碼示例
- C++ Algorithm pop_heap()用法及代碼示例
- C++ Algorithm set_intersection()用法及代碼示例
- C++ Algorithm lower_bound()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Algorithm rotate()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。