c++stl和std
C ++ STL std :: rotate()函数 (C++ STL std::rotate() function)
rotate() function is a library function of algorithm header, it is used to rotate left the elements of a sequence within a given range, it accepts the range (start, end) and a middle point, it rotates the elements in such way that the element pointed by the middle iterator becomes the new first element.
rotation()函数是算法标头的库函数,用于在给定范围内向左旋转序列的元素,接受范围(开始,结束)和中间点,以这种方式旋转元素中间迭代器指向的元素将成为新的第一个元素。
Note: To use rotate() function – include <algorithm> header or you can simple use <bits/stdc++.h> header file.
注意:要使用rotate()函数 –包括<algorithm>头文件,或者您可以简单地使用<bits / stdc ++。h>头文件。
Syntax of std::rotate() function
std :: rotate()函数的语法
std::rotate(iterator start, iterator middle, iterator end);
Parameter(s):
参数:
iterator start – an iterator pointing to the first element of the sequence.
迭代器开始 –指向序列第一个元素的迭代器。
iterator middle – an iterator pointing to the middle or any other elements from where we want to start the rotation.
中间迭代器 –指向中间或我们要开始旋转的位置的任何其他元素的迭代器。
iterator end – an iterator pointing to the last element of the sequence.
迭代器末端 –指向序列的最后一个元素的迭代器。
Return value: void – it returns noting.
返回值: void –返回注释。
Example:
例:
Input:
vector<int> v{ 10, 20, 30, 40, 50 };
//rotating vector from 2nd element
rotate(v.begin(), v.begin() + 2, v.end());
Output:
30 40 50 10 20
C ++ STL程序演示了std :: rotate()函数的使用 (C++ STL program to demonstrate use of std::rotate() function)
In this program, we have a vector and we are rotating its elements from 2nd index.
在此程序中,我们有一个向量,并从第二个索引开始旋转其元素。
//C++ STL program to demonstrate use of
//std::rotate() function
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//main code
int main()
{
//vector
vector<int> v{ 10, 20, 30, 40, 50 };
//printing vector elements
cout << "vector elements begfore rotating..." << endl;
for (int x : v)
cout << x << " ";
cout << endl;
//rotating vector from 2nd element
rotate(v.begin(), v.begin() + 2, v.end());
cout << "vector elements after rotating..." << endl;
for (int x : v)
cout << x << " ";
cout << endl;
return 0;
}
Output
输出量
vector elements begfore rotating...
10 20 30 40 50
vector elements after rotating...
30 40 50 10 20
Reference: C++ std::rotate()
参考: C ++ std :: rotate()
翻译自: https://www.includehelp.com/stl/std-rotate-function-with-example.aspx
c++stl和std