STL基本用法学习————rotate
参考cppreference——rotate
cplusplus——rotate
基本语法:
template< class ForwardIt >
ForwardIt rotate( ForwardIt first, ForwardIt n_first, ForwardIt last );
将范围在[first,last)内的元素进行旋转,主要操作是将n_first的元素放在这一列范围的最前方,将 [first, n_first-1]的元素放在这一列的最后。
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v;
std::vector<int> v2;
for (int i = 0; i < 10; ++i) v.push_back(i);
for (int i = 0; i < 10; ++i) v2.push_back(i);
std::rotate(v.begin(), v.begin() + 4, v.end());
std::rotate(v2.begin(), v2.begin() + 4, v2.begin() + 5);
for (auto x : v) {
std::cout << x << " ";
}
std::cout << std::endl;
//cout:4 5 6 7 8 9 0 1 2 3
for (auto x : v2) {
std::cout << x << " ";
}
std::cout << std::endl;
//4 0 1 2 3 5 6 7 8 9
return 0;
}