How to perform a circular shift of the elements of a vector to the right in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    
    // rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
    std::rotate(vec.begin(), vec.begin() + vec.size() - 1, vec.end());
    
    for (const auto& element : vec) {
        std::cout << element << " ";
    }
}


 
/*
run:
 
9 0 1 2 3 4 5 6 7 8 
 
*/

 



answered Jul 5, 2024 by avibootz
...