Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. template<typename T> void rotate(T begin, T end, int k)
  9. {
  10. int n = end - begin;
  11. k = (k + n) % n;
  12.  
  13. for (int i = 0; i < k; i ++)
  14. {
  15. auto value = *(end-1);
  16. for (T curIter = end-1; curIter != begin; curIter --)
  17. *curIter = *(curIter - 1);
  18. *begin = value;
  19. }
  20. }
  21.  
  22. int main() {
  23. int a[] = {1, 2, 3, 4, 5};
  24. rotate (a, a + 5, -2);
  25.  
  26. for (int i = 0; i < 5; i++)
  27. cout << a[i] << ' ';
  28. cout << endl;
  29.  
  30.  
  31. vector<int> b{1,2,3,4,5};
  32. rotate (b.begin(), b.end(), -2);
  33. for (int i = 0; i < 5; i++)
  34. cout << b[i] << ' ';
  35. cout << endl;
  36.  
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement