How to circularly rotate a vector to right by N places in C++

1 Answer

0 votes
#include <vector>
#include <iostream>
#include <algorithm>
  
int main()
{
    std::vector<int> vec {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int N = 3;

     std::rotate(vec.rbegin(), vec.rbegin() + N, vec.rend());
    
    for (auto n : vec) {
        std::cout << n << ' ';
    }
}
  
  
  
  
/*
run:
  
7 8 9 0 1 2 3 4 5 6 
  
*/

 



answered Nov 28, 2023 by avibootz

Related questions

2 answers 91 views
1 answer 80 views
2 answers 108 views
1 answer 89 views
1 answer 122 views
1 answer 644 views
...