c++stl和std_std :: rotate()函数以及C ++ STL中的示例
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 elementrotate(v.begin(), v.begin() + 2, v.end());Output:30 40 50 10 20C ++ 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() {//vectorvector<int> v{ 10, 20, 30, 40, 50 };//printing vector elementscout << "vector elements begfore rotating..." << endl;for (int x : v)cout << x << " ";cout << endl;//rotating vector from 2nd elementrotate(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 20Reference: C++ std::rotate()
參考: C ++ std :: rotate()
翻譯自: https://www.includehelp.com/stl/std-rotate-function-with-example.aspx
c++stl和std
總結
以上是生活随笔為你收集整理的c++stl和std_std :: rotate()函数以及C ++ STL中的示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++一个类创建多个对象_C ++ |
- 下一篇: Python operator.lt()