std::rotate
|
|
Esta página se ha traducido por ordenador/computador/computadora de la versión en inglés de la Wiki usando Google Translate.
La traducción puede contener errores y palabras aparatosas/incorrectas. Planea sobre el texto para ver la versión original. Puedes ayudar a corregir los errores y mejorar la traducción. Para instrucciones haz clic aquí. |
| Definido en el archivo de encabezado <algorithm>
|
||
| template< class ForwardIt > void rotate( ForwardIt first, ForwardIt n_first, ForwardIt last ); |
(hasta C++11) (desde C++11) |
|
[first, last) rango de tal manera, que la n_first elemento se convierte en el primer elemento de la nueva gama y n_first - 1 se convierte en el último elemento . [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. You can help to correct and verify the translation. Click here for instructions.
Contenido |
[editar] Parámetros
| first, last | - | la gama de elementos a girar
Original: the range of elements to rotate The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| n_first | - | el elemento se mueva hasta el principio de la nueva gama
Original: the element to move to the beginning of the new range The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| Requerimientos de tipo | ||
-ForwardIt debe reunir los requerimientos de ValueSwappable y ForwardIterator.
| ||
-The type of dereferenced ForwardIt must meet the requirements of MoveAssignable and MoveConstructible.
| ||
[editar] Valor de retorno
You can help to correct and verify the translation. Click here for instructions.
first + (last - n_first) (desde C++11)first + (last - n_first) (desde C++11)You can help to correct and verify the translation. Click here for instructions.
[editar] Complejidad
first y lastfirst and lastYou can help to correct and verify the translation. Click here for instructions.
[editar] Posible implementación
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; } } } |
[editar] Ejemplo
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'; }
Salida:
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
[editar] Ver también
| copias y girar una serie de elementos Original: copies and rotate a range of elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (plantilla de función) |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
