C++ examples for STL Algorithm:rotate
Right-rotate a sequence by using reverse iterators with the rotate() algorithm.
#include <iostream> #include <vector> #include <algorithm> using namespace std; void show(const char *msg, vector<int> vect); int main()//from ww w . j a va 2s. co m { vector<int> v; for(int i=0; i<10; i++) v.push_back(i); show("Original order: ", v); cout << endl; // Rotate right two positions using reverse iterators. rotate(v.rbegin(), v.rbegin()+2, v.rend()); show("Order after two right-rotates: ", v); return 0; } void show(const char *msg, vector<int> vect) { cout << msg; for(unsigned i=0; i < vect.size(); ++i) cout << vect[i] << " "; cout << "\n"; }