Or login with:
#include <algorithm> template < class ForwardIterator, class OutputIterator > OutputIterator rotate_copy( ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result );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 |
| result | An output iterator addressing the position of the first element in the destination range |
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int a1[] = {1, 2, 3, 4, 5}; vector<int> v1(a1, a1+5); cout <<"\nHere are the values in the vector v1:\n"; for (vector<int>::size_type i=0; i<v1.size(); i++) cout <<v1.at(i)<<" "; int a2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; vector<int> v2(a2, a2+10); cout <<"\nHere are the values in the vector v2:\n"; for (vector<int>::size_type i=0; i<v2.size(); i++) cout <<v2.at(i)<<" "; cout <<"\nNow we copy all values from v1 to v2, starting at the " "beginning of v2,\nand simultaneously rotating the order of the " "copied values so that the\nfourth value bcomes the first value."; vector<int>::iterator p; p = rotate_copy(v1.begin(), v1.begin()+3, v1.end(), v2.begin()); cout <<"\nHere are the revised values in v2:\n"; for (vector<int>::size_type i=0; i<v2.size(); i++) cout <<v2.at(i)<<" "; cout <<"\nThe iterator returned by the algorithm is pointing at the value" <<*p<<"."; cout <<"\nNow we copy the midddle 3 of the values in v1 from v1 to " "v2, starting at the\n7th value of v2, and simultaneously rotate " "the order of the copied values so\nthat the 3rd of the copied " "values becomes the first of the copied group."; p = rotate_copy(v1.begin()+1, v1.begin()+3, v1.end()-1, v2.begin()+6); cout <<"\nHere are the revised values in v2:\n"; for (vector<int>::size_type i=0; i<v2.size(); i++) cout <<v2.at(i)<<" "; cout <<"\nThe iterator returned by the algorithm is pointing at the value" <<*p<<"."; return 0; }
You must login to leave a messge