std::rotate
时间: 2023-09-15 07:20:05 浏览: 47
`std::rotate` is a function in the C++ Standard Library that rotates the elements in a range by a given number of positions.
The function takes three arguments:
1. A pair of iterators that define the range of elements to be rotated.
2. An iterator that points to the element that should be moved to the beginning of the range.
3. (Optional) An iterator that points to the end of the range. If not specified, the end of the range is assumed to be the iterator that follows the last element to be rotated.
The function shifts the elements in the specified range so that the element pointed to by the second argument becomes the first element in the range. The remaining elements are shifted to the right, with the last element in the range becoming the element that was previously at the position pointed to by the second argument.
Here's an example of how to use `std::rotate`:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> v{1, 2, 3, 4, 5};
std::rotate(v.begin(), v.begin() + 2, v.end());
for (int i : v) {
std::cout << i << ' ';
}
std::cout << '\n';
}
```
This will output:
```
3 4 5 1 2
```
In this example, we rotate the elements in the vector `v` so that the element at position 2 (`3`) becomes the first element in the range. The resulting vector is `{3, 4, 5, 1, 2}`.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)