IMSoP
1
Q:

rotate vector c++

// LEFT ROTATION
std::vector<int> vec1{1,2,3,4,5,6,7,8,9}; // works for std::string as well
// Rotate vector left 3 times.
int rotL=3; 
  
// std::rotate function 
std::rotate(vec1.begin(), vec1.begin()+rotL, vec1.end()); 
// vec1 = {4, 5, 6, 7, 8, 9, 1, 2, 3};

// RIGHT ROTATION
std::vector<int> vec1{1,2,3,4,5,6,7,8,9}; // // works for std::string as well
// Rotate vector right 4 times. 
int rotR = 4; 
  
// std::rotate function 
std::rotate(vec2.begin(), vec2.begin()+vec2.size()-rotR, vec2.end());
// vec2 = {6, 7, 8, 9, 1, 2, 3, 4, 5};
0

New to Communities?

Join the community