Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<algorithm>
- using namespace std;
- template<typename T> void rotate(T begin, T end, int k)
- {
- int n = end - begin;
- k = (k + n) % n;
- for (int i = 0; i < k; i ++)
- {
- auto value = *(end-1);
- for (T curIter = end-1; curIter != begin; curIter --)
- *curIter = *(curIter - 1);
- *begin = value;
- }
- }
- int main() {
- int a[] = {1, 2, 3, 4, 5};
- rotate (a, a + 5, -2);
- for (int i = 0; i < 5; i++)
- cout << a[i] << ' ';
- cout << endl;
- vector<int> b{1,2,3,4,5};
- rotate (b.begin(), b.end(), -2);
- for (int i = 0; i < 5; i++)
- cout << b[i] << ' ';
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement