How to perform a circular shift of the elements of an array to the left in C++

1 Answer

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

#define LEN 10

int main() {
    unsigned int arr[LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    
    // rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
    std::rotate(&arr[0], &arr[1], &arr[LEN]);
    
    for (int i = 0; i < LEN; i++) {
        std::cout << "arr[" << i << "] = " << arr[i] << "\n";
    }
}


 
/*
run:
 
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
arr[5] = 6
arr[6] = 7
arr[7] = 8
arr[8] = 9
arr[9] = 0
 
*/

 



answered Jul 5, 2024 by avibootz
edited Jun 26, 2025 by avibootz
...