Guest User

Untitled

a guest
Dec 15th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5.  
  6. template <typename T>
  7. void modify_k(T begin, T end, int & k) {
  8. int length = 0;
  9. T it = begin;
  10. while(it != end) {
  11. length++;
  12. it++;
  13. }
  14. k = (k % length + length) % length;
  15. }
  16.  
  17. template <typename T>
  18. void rotate(T begin, T end, int k) {
  19. modify_k(begin, end, k);
  20. T middle = begin + k;
  21. T next = middle;
  22. T left, right;
  23. while(begin != next) {
  24. left = begin;
  25. right = next;
  26. begin++;
  27. next++;
  28. swap(*left, *right);
  29. if(next == end) {
  30. next = middle;
  31. } else {
  32. if(begin == middle) {
  33. middle = next;
  34. }
  35. }
  36. }
  37. }
  38.  
  39. int main() {
  40. freopen("input.txt", "r", stdin);
  41. freopen("output.txt", "w", stdout);
  42.  
  43. vector<int> a;
  44. for(int i = 0; i < 10; ++i)
  45. a.push_back(i + 1);
  46.  
  47. rotate(a.begin(), a.end(), 3);
  48.  
  49. cout << "VECTOR\n";
  50. for(int i = 0; i < 10; ++i)
  51. cout << a[i] << endl;
  52.  
  53.  
  54. int b[10];
  55. for(int i = 0; i < 10; ++i)
  56. b[i] = i + 1;
  57.  
  58. rotate(b, b + 10, -2);
  59.  
  60. cout << "\n\nMASSIVE\n";
  61. for(int i = 0; i < 10; ++i)
  62. cout << b[i] << endl;
  63.  
  64. return 0;
  65. }
Add Comment
Please, Sign In to add comment