Or login with:
#include <algorithm> template < class ForwardIterator > void rotate( ForwardIterator first, ForwardIterator middle, ForwardIterator last );Parameters
| Parameter | Description |
|---|---|
| first | A forward iterator addressing the position of the first element in the range to be rotated |
| middle | A forward iterator defining the boundary within the range that addresses the position of the first element in the second part of the range whose elements are to be exchanged with those in the first part of the range |
| last | A forward iterator addressing the position one past the final element in the range to be rotated |
first (it appends the element between the first and the middle element to the elements between the middle and the last element).#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; vector<int> v(a, a+10); cout <<"\nHere are the contents of the vector:\n"; for (vector<int>::size_type i=0; i<v.size(); i++) cout <<v.at(i)<<" "; cout <<"\nNow we rotate the order of all values in the vector " "\nin so that the fourth value becomes the first value."; rotate(v.begin(), v.begin()+3, v.end()); cout <<"\nHere are the revised contents of the vector:\n"; for (vector<int>::size_type i=0; i<v.size(); i++) cout <<v.at(i)<<" "; cout <<"\nNext we rotate the order of the 2nd to the 6th values " "in the vector so that\nthe third value in that group of values " "becomes the first value of the group."; rotate(v.begin()+1, v.begin()+3, v.begin()+6); cout <<"\nHere are the revised contents of the vector:\n"; for (vector<int>::size_type i=0; i<v.size(); i++) cout <<v.at(i)<<" "; return 0; }
You must login to leave a messge