Shift Elements in Array in C++

  1. Use std::rotate Algorithm to Shift Elements in Array in C++
  2. Use the Custom Wrapper Function for std::rotate to Shift Elements in Array in C++
  3. Use the std::rotate_copy Algorithm to Shift Elements in Array in C++

This article will introduce several methods of how to shift elements in the array in C++.

Use std::rotate Algorithm to Shift Elements in Array in C++

The std::rotate function is part of the C++ algorithms library that can be imported using the <algorithm> header. This algorithm rotates the array elements to the left side. It takes three parameters of iterator types, the second of which specifies the element that needs to be the first element of the newly constructed range. Meanwhile, the first and the third elements are the source range specifiers for beginning and end positions.

Notice that std::rotate can be utilized to shift elements to the right side using the rbegin/rend iterators. In the following example, the function is called on the std::vector object with 10 integers, and operations for both directions are demonstrated.

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>

using std::cout; using std::vector;
using std::array; using std::rotate;
using std::endl;

template<typename T>
void printElements(T &v)
{
    cout << "[ ";
    for (const auto &item : v) {
        cout << item << ", ";
    }
    cout << "\b\b ]" << endl;
}

int main()
{
    vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    printElements(vec);
    rotate(vec.begin(), vec.begin() + 3, vec.end());

    printElements(vec);
    rotate(vec.rbegin(), vec.rbegin() + 3, vec.rend());

    exit(EXIT_SUCCESS);
}

Output:

[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
[ 4, 5, 6, 7, 8, 9, 10, 1, 2, 3 ]

Use the Custom Wrapper Function for std::rotate to Shift Elements in Array in C++

Alternatively, we can implement the wrapper function to encapsulate the std::rotate algorithm and require the user to pass only 2 arguments - the array object to be rotated and the integer representing the number of positions to shift. We can also denote the sign of the passed integer as the direction to which the rotation operation should be processed.

In this custom function, we arbitrarily chose the positive integer to mean the right rotation and the negative to mean the left rotation.

Note that this rotateArrayElements function template can work on both fixed and dynamic array objects constructed with the C++ standard library containers.

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>

using std::cout; using std::vector;
using std::array; using std::rotate;
using std::endl;

template<typename T>
void printElements(T &v)
{
    cout << "[ ";
    for (const auto &item : v) {
        cout << item << ", ";
    }
    cout << "\b\b ]" << endl;
}

template<typename T>
int rotateArrayElements(T &v, int dir)
{
    if (dir > 0) {
        rotate(v.rbegin(), v.rbegin() + dir, v.rend());
        return 0;
    } else if (dir < 0) {
        rotate(v.begin(), v.begin() + abs(dir), v.end());
        return 0;
    } else {
        return 1;
    }
}

int main()
{
    array<int, 10> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    rotateArrayElements(arr, 3);
    printElements(arr);

    rotateArrayElements(vec, -3);
    printElements(vec);


    exit(EXIT_SUCCESS);
}

Output:

[ 8, 9, 10, 1, 2, 3, 4, 5, 6, 7 ]
[ 4, 5, 6, 7, 8, 9, 10, 1, 2, 3 ]

Use the std::rotate_copy Algorithm to Shift Elements in Array in C++

std::rotate_copy algorithm implements the same operation as the std::rotate except that the former copies the rotated array elements to another range specified with additional function parameter.

At first, we need to declare the new range, in this case, std::vector type is chosen, and the constructor takes the size of the source vector.

We can then call the rotate_copy function with the same parameters as we would specify for std::rotate and the fourth iterator denoting the beginning of the destination vector.

Notice that the following example only demonstrates the left rotation of the array elements.

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>

using std::cout; using std::vector;
using std::array; using std::rotate;
using std::endl; using std::rotate_copy;

template<typename T>
void printElements(T &v)
{
    cout << "[ ";
    for (const auto &item : v) {
        cout << item << ", ";
    }
    cout << "\b\b ]" << endl;
}

int main()
{
    vector<int> vec1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    printElements(vec1);
    vector<int> vec2(vec1.size());
    rotate_copy(vec1.begin(), vec1.begin() + 3, vec1.end(), vec2.begin());
    printElements(vec2);

    exit(EXIT_SUCCESS);
}
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
[ 4, 5, 6, 7, 8, 9, 10, 1, 2, 3 ]

Related Article - C++ Array

  • Create Array of Structs in C++
  • Return an Array From a Function in C++