1、rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last):等效于循环左移序列,使得迭代器middle所指的元素成为首元素。
std::vector<int> c; c.reserve(10); //向c中添加元素 for (int i = 0; i < 10; i++) { c.push_back(i); } //旋转c中的元素,使得数组下标为5的元素成为首元素 std::rotate(c.begin(), c.begin() + 5, c.end()); //输出c for (auto var : c) { std::cout << var << ","; } //打印结果:5,6,7,8,9,0,1,2,3,4
2、rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result):等效于循环左移序列并复制到新的存储空间,使得迭代器middle所指的元素成为首元素。
std::vector<int> c; std::vector<int> result; c.reserve(10); result.resize(10); //向c中添加元素 for (int i = 0; i < 10; i++) { c.push_back(i); } //复制c的旋转到result中,c本身没有改变 std::rotate_copy(c.begin(), c.begin() + 5, c.end(), result.begin()); //输出result for (auto var : result) { std::cout << var << ","; } //打印结果:5,6,7,8,9,0,1,2,3,4