#include <iostream>
#include <vector>
#include <algorithm>
// Function to print the content of the vector
void print_vector(const std::vector<int>& vec) {
for (int elem : vec) {
std::cout << elem << " ";
}
std::cout << std::endl;
}
// Function to perform a cyclic shift in a one-dimensional ring array
void cyclic_shift(std::vector<int>& arr, int steps) {
// Find the shift position taking into account the size of the array
int shift_position = steps % arr.size();
// Rotate the array using the std::rotate function from the <algorithm> library
if (shift_position > 0) {
std::rotate(arr.rbegin(), arr.rbegin() + shift_position, arr.rend());
} else if (shift_position < 0) {
std::rotate(arr.begin(), arr.begin() - shift_position, arr.end());
}
}
int main() {
std::vector<int> arr = {1, 2, 3, 4, 5, 6, 7};
std::cout << "Original Array: ";
print_vector(arr);
int steps = 2;
cyclic_shift(arr, steps);
std::cout << "Right Shifted by " << steps << " steps: ";
print_vector(arr);
steps = -3;
cyclic_shift(arr, steps);
std::cout << "Left Shifted by " << -steps << " steps: ";
print_vector(arr);
return 0;
}
This code defines a cyclic_shift function that takes an std::vector<int> and the number of steps to shift as arguments. The std::rotate function is used to perform the shift. The code also includes a print_vector function to print the content of a vector, and the main function demonstrates how to use the cyclic_shift function.