Skip to content

Instantly share code, notes, and snippets.

@jin-x
Created December 27, 2019 10:56
Show Gist options
  • Save jin-x/d3926eaeea5ab50051fa15f8130199c1 to your computer and use it in GitHub Desktop.
Save jin-x/d3926eaeea5ab50051fa15f8130199c1 to your computer and use it in GitHub Desktop.
@jinxonik / UniLecs #202
#include <iostream>
#include <vector>
#include <algorithm>
using std::cout;
using std::vector;
template <typename T>
void shift_array(vector<T>& array, size_t value)
{
size_t total = array.size();
if (value > total) {
if (total == 0) { return; }
value %= total;
}
if (value == 0) { return; }
std::reverse(array.begin(), std::prev(array.end(), value));
std::reverse(std::prev(array.end(), value), array.end());
std::reverse(array.begin(), array.end());
}
int main()
{
vector<int> arr { 1, -2, 3, -4, 5, -6, 7, -8, 9, -10 };
shift_array(arr, 3);
cout << "{ ";
for (int n : arr) {
cout << n << " ";
}
cout << "}\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment