std::rotate - cppreference.com
De cppreference.com
<metanoindex/>
<tbody> </tbody>
| Definido no cabeçalho <algorithm> |
||
|
|
(até C++11) (desde C++11) |
|
Troca os elementos da gama [first, last) de tal maneira, que o elemento n_first torna-se o primeiro elemento da nova gama e n_first - 1 torna-se o elemento anterior.
Original:
Swaps the elements in the range [first, last) in such a way, that the element n_first becomes the first element of the new range and n_first - 1 becomes the last element.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Parâmetros
| first, last | - | a gama de elementos de girar Original: the range of elements to rotate The text has been machine-translated via Google Translate. |
| n_first | - | o elemento para mover para o início do intervalo de novo Original: the element to move to the beginning of the new range The text has been machine-translated via Google Translate. |
| Type requirements | ||
-ForwardIt must meet the requirements of ValueSwappable and ForwardIterator.
| ||
-The type of dereferenced ForwardIt must meet the requirements of MoveAssignable and MoveConstructible.
| ||
Valor de retorno
(Nenhum) (até C++11)
Original:
(none) (até C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
O iterador igual a first + (last - n_first) (desde C++11)
Original:
The iterator equal to first + (last - n_first) (desde C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Complexidade
linear, em que a distância entre first e last
Original:
linear in the distance between first and last
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Possível implementação
template<class ForwardIt> void rotate(ForwardIt first, ForwardIt n_first, ForwardIt last) { ForwardIt next = n_first; while (first != next) { std::swap(*first++, *next++); if (next == last) { next = n_first; } else if (first == n_first) { n_first = next; } } }
Exemplo
std :: rotação é um bloco de construção comum em muitos algoritmos. Este exemplo demonstra o tipo de inserção de C + +
Original:
std::rotate is a common building block in many algorithms. This example demonstrates insertion sort in C++
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#include <vector> #include <iostream> #include <algorithm> int main() { std::vector<int> v{2, 4, 2, 0, 5, 10, 7, 3, 7, 1}; std::cout << "before sort: "; for(int n: v) std::cout << n << ' '; std::cout << '\n'; // insertion sort for (auto i = v.begin(); i != v.end(); ++i) { std::rotate(std::upper_bound(v.begin(), i, *i), i, i+1); } std::cout << "after sort: "; for(int n: v) std::cout << n << ' '; std::cout << '\n'; // simple rotation to the left std::rotate(v.begin(), v.begin() + 1, v.end()); std::cout << "simple rotate left : "; for(int n: v) std::cout << n << ' '; std::cout << '\n'; // simple rotation to the right std::rotate(v.rbegin(), v.rbegin() + 1, v.rend()); std::cout << "simple rotate right : "; for(int n: v) std::cout << n << ' '; std::cout << '\n'; }
Saída:
before sort: 2 4 2 0 5 10 7 3 7 1 after sort: 0 1 2 2 3 4 5 7 7 10 simple rotate left : 1 2 2 3 4 5 7 7 10 0 simple rotate right: 0 1 2 2 3 4 5 7 7 10
Veja também
cópias e rodar uma série de elementos Original: copies and rotate a range of elements The text has been machine-translated via Google Translate. (modelo de função) [edit] | |