Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. enum class Direction { Left, Right };
  6.  
  7. void shiftImage(std::vector<std::vector<int>> &mat, int shift, Direction dir)
  8. {
  9. if (dir == Direction::Left) {
  10. for (auto &row : mat) {
  11. std::rotate(row.rbegin(), row.rbegin() + shift, row.rend());
  12. }
  13. return;
  14. }
  15.  
  16. for (auto &row : mat) {
  17. std::rotate(row.begin(), row.begin() + shift, row.end());
  18. }
  19. }
  20.  
  21. void print(const std::vector<std::vector<int>> &mat)
  22. {
  23. for (const auto &y : mat) {
  24. for (const auto &j : y) {
  25. std::cout << j << " ";
  26. }
  27. std::cout << "\n";
  28. }
  29. std::cout << "\n";
  30. }
  31.  
  32. int main()
  33. {
  34. std::vector<std::vector<int>> a{
  35. {1, 2, 3, 4, 5},
  36. {1, 2, 3, 4, 5},
  37. {1, 2, 3, 4, 5}
  38. };
  39.  
  40. std::vector<std::vector<int>> b(a);
  41.  
  42. print(a);
  43.  
  44. shiftImage(a, 1, Direction::Left);
  45. print(a);
  46.  
  47. shiftImage(b, 1, Direction::Right);
  48. print(b);
  49.  
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement