[C/C++] rotate()

Onam Kwon·2022년 11월 28일
0

C/C++

목록 보기
4/12

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);

    // Rotating a given vector to the left direction by 1.
    // Require algorithm library. 
    rotate(v.begin(), v.begin()+1, v.end());
    print("After rotating to the left direction: ", v);

    // Rotating a given vector to the right direction by 2. 
    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

profile
권오남 / Onam Kwon

0개의 댓글