fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. void print(const std::vector<int>& vec) {
  5. for(int el : vec)
  6. std::cout << el << " ";
  7. std::cout << std::endl;
  8. }
  9.  
  10. int main() {
  11. std::vector<int> vec = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  12.  
  13. print(vec);
  14.  
  15. for(int i = 0; i < vec.size(); i += 4) {
  16. if (i + 3 > vec.size() - 1)
  17. break;
  18.  
  19. std::swap(vec[i], vec[i + 3]);
  20. }
  21.  
  22. print(vec);
  23.  
  24. return 0;
  25. }
Success #stdin #stdout 0s 4228KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6 7 8 9 
4 2 3 1 8 6 7 5 9