fork download
  1. #include<vector>
  2. #include<iostream>
  3. #include<algorithm>
  4.  
  5. int main() {
  6. std::vector<int> values{0, 10, 20, 30, 40, 50, 60, 70, 80, 90};
  7. std::vector<size_t> indexes_to_move{2,4};
  8. size_t destination_index = 6;
  9. if(destination_index > values.size()) throw std::runtime_error("Come on, bro.");
  10.  
  11. for(auto const& val : values) std::cout << val << ',';
  12. std::cout << std::endl;
  13.  
  14. for(size_t _index = 0; _index < indexes_to_move.size(); _index++) {
  15. size_t index = indexes_to_move[indexes_to_move.size() - _index - 1]; //We need to iterate in reverse.
  16. if(index >= values.size()) throw std::runtime_error("We dun goofed.");
  17. if(index >= destination_index) throw std::runtime_error("We goofed in a different way.");
  18.  
  19. std::rotate(values.begin() + index, values.begin() + index + 1, values.begin() + destination_index);
  20. destination_index--;
  21. }
  22.  
  23. for(auto const& val : values) std::cout << val << ',';
  24. std::cout << std::endl;
  25. return 0;
  26. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
0,10,20,30,40,50,60,70,80,90,
0,10,30,50,20,40,60,70,80,90,