Just Learn Code

Array Shifting in C++: std::rotate Custom Wrappers and std::rotate_copy

Shifting Elements in Arrays in C++ – An In-Depth Exploration

As programmers, one of the most common tasks that we come across is the manipulation of arrays. Arrays are collections of variables that hold similar or related values.

The elements in an array are stored in contiguous memory locations, allowing us to access them using the array’s name and an index number. One of the most common operations that we can perform with an array is shifting its elements to the left or right.

In this article, we’ll be exploring ways of shifting elements in arrays in C++, using the std::rotate algorithm, a custom wrapper function for std::rotate, and the std::rotate_copy algorithm. Using std::rotate Algorithm

The std::rotate algorithm is part of the C++ Standard Template Library (STL) and is used to rotate the elements in a range of values.

This algorithm takes two iterator types that specify the range of values to rotate and a third iterator type that specifies the position of the new beginning of the range. Here’s an example of how to use std::rotate for left rotation:

“`

#include

#include

int main() {

int myArray[] = {1, 2, 3, 4, 5};

std::rotate(std::begin(myArray), std::begin(myArray) + 1, std::end(myArray));

for (auto x : myArray) std::cout << x << " "; // output: 2 3 4 5 1

return 0;

}

“`

In the example above, we have an array of integers called `myArray`.

We pass the iterators for the beginning and end of the array, along with an iterator that points to the position after the first element (i.e., the second element in the array) to std::rotate. After the std::rotate function call, the elements in `myArray` are shifted to the left by one position.

The output of the program shows the new order of elements in `myArray`. Similarly, for right-rotation, you just have to adjust the iterator arguments accordingly.

Alternatively, for right rotation, you can also specify the position of the new end of the range, as shown below:

“`

#include

#include

int main() {

int myArray[] = {1, 2, 3, 4, 5};

std::rotate(std::rbegin(myArray), std::rbegin(myArray) + 1, std::rend(myArray));

for (auto x : myArray) std::cout << x << " "; // output: 5 1 2 3 4

return 0;

}

“`

In the example above, we have replaced the `begin` and `end` iterators with `rbegin` and `rend`, respectively. This is because we want to start rotating the elements from the right side of the array.

The rest of the code remains the same. Using Custom Wrapper Function for std::rotate

While the std::rotate algorithm is simple to use, it can be challenging to write in certain scenarios.

This is where a custom wrapper function can come in handy. A custom wrapper function encapsulates the std::rotate algorithm with additional functionality or custom argument handling, making it more user-friendly.

Here’s an example of a custom wrapper function for std::rotate:

“`

#include

#include

template

void rotateArray(T (&arr)[N], int shift, bool left = true) {

if (left) std::rotate(std::begin(arr), std::begin(arr) + shift, std::end(arr));

else std::rotate(std::rbegin(arr), std::rbegin(arr) + shift, std::rend(arr));

}

int main() {

int myArray[] = {1, 2, 3, 4, 5};

rotateArray(myArray, 2, false);

for (auto x : myArray) std::cout << x << " "; // output: 4 5 1 2 3

return 0;

}

“`

In the code above, we define a custom wrapper function called `rotateArray`. This function takes an array of any type and size and two integer arguments: `shift` and `left`.

The `shift` argument controls the number of positions to shift the array, while the `left` argument controls the direction of rotation. By default, the function performs a left rotation.

If we change the `left` argument to `false`, the function performs a right rotation. Using std::rotate_copy Algorithm

The std::rotate_copy algorithm is similar to std::rotate.

However, instead of modifying an array in place, the algorithm creates a new range of values, with rotated elements, in a separate container. Here’s an example of how to use std::rotate_copy with a std::vector:

“`

#include

#include

#include

int main() {

std::vector myVector = {1, 2, 3, 4, 5};

std::vector result(myVector.size());

std::rotate_copy(std::begin(myVector), std::begin(myVector) + 2, std::end(myVector), std::begin(result));

for (auto x : result) std::cout << x << " "; // output: 3 4 5 1 2

return 0;

}

“`

In the code above, we have a std::vector called `myVector`.

We pass the iterators for the beginning and end of the vector, along with an iterator that points to the position after the second element to std::rotate_copy. The `std::begin(result)` argument specifies the beginning of the destination vector.

After the std::rotate_copy function call, a new vector called `result` is created with the rotated elements. The output of the program shows the contents of the `result` vector.

Example Code

Here’s an example of how to rotate the elements in an array using std::rotate, std::rotate_copy, and a custom wrapper function:

“`

#include

#include

#include

template

void rotateArray(T (&arr)[N], int shift, bool left = true) {

if (left) std::rotate(std::begin(arr), std::begin(arr) + shift, std::end(arr));

else std::rotate(std::rbegin(arr), std::rbegin(arr) + shift, std::rend(arr));

}

int main() {

int myArray[] = {1, 2, 3, 4, 5};

std::vector myVector = {6, 7, 8, 9, 10};

std::vector result(myVector.size());

std::cout << "Before rotation:" << std::endl;

for (auto x : myArray) std::cout << x << " ";

std::cout << std::endl;

// left rotation with std::rotate

std::rotate(std::begin(myArray), std::begin(myArray) + 2, std::end(myArray));

std::cout << "Left rotated with std::rotate:" << std::endl;

for (auto x : myArray) std::cout << x << " ";

std::cout << std::endl;

// right rotation with std::rotate_copy

std::rotate_copy(std::begin(myArray), std::begin(myArray) + 2, std::end(myArray), std::begin(result));

std::cout << "Right rotated with std::rotate_copy:" << std::endl;

for (auto x : result) std::cout << x << " ";

std::cout << std::endl;

// left rotation with custom wrapper function

rotateArray(myArray, 2, true);

std::cout << "Left rotated with custom wrapper function:" << std::endl;

for (auto x : myArray) std::cout << x << " ";

std::cout << std::endl;

// right rotation with std::rotate_copy

std::rotate_copy(std::begin(myVector), std::begin(myVector) + 2, std::end(myVector), std::begin(result));

std::cout << "Right rotated with std::rotate_copy:" << std::endl;

for (auto x : result) std::cout << x << " ";

std::cout << std::endl;

return 0;

}

“`

The output of the program is as follows:

“`

Before rotation:

1 2 3 4 5

Left rotated with std::rotate:

3 4 5 1 2

Right rotated with std::rotate_copy:

5 1 2 3 4

Left rotated with custom wrapper function:

4 5 1 2 3

Right rotated with std::rotate_copy:

8 9 10 6 7

“`

Conclusion

In this article, we’ve explored three ways to shift elements in arrays in C++. We’ve covered the std::rotate and std::rotate_copy algorithms, as well as a custom wrapper function for std::rotate.

By mastering these techniques, you can optimize your array manipulation code and create efficient, reliable programs that can handle any array processing tasks that come your way. In this article, we explored three ways to shift elements in arrays in C++ using STD::rotate algorithm, Custom wrapper function, and STD::rotate_copy algorithm.

These techniques can help optimize array manipulation code and create efficient, reliable programs that can handle any array processing tasks that come your way. This topic is crucial to understand for all programmers who need to manipulate arrays in their code.

By mastering these techniques, programmers can create efficient code and improve the performance of their programs.

Popular Posts