rotate()
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void print(string s, vector<int> &v) {
cout<<s;
for(const auto &item: v) cout<<item<<" "; cout<<endl;
}
int main() {
vector<int> v = {1,2,3,4,5};
print("Before rotating: ", v);
rotate(v.begin(), v.begin()+1, v.end());
print("After rotating to the left direction: ", v);
rotate(v.rbegin(), v.rbegin()+2, v.rend());
print("After rotating to the right direction: ", v);
return 0;
}
~/De/D/C/P/Algorithms/A/RotateVector main ?1 ❯ g++ main.cpp -o main.out
~/De/D/C/P/Algorithms/A/RotateVector main ?1 ❯ ./main.out 22:19:58
Before rotating: 1 2 3 4 5
After rotating to the left direction: 2 3 4 5 1
After rotating to the right direction: 5 1 2 3 4
GitHub source code