◐ Shell
clean mode source ↗

std::reverse - cppreference.com

De cppreference.com

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 BidirIt > void reverse( BidirIt first, BidirIt last );

Invierte el orden de los elementos en el rango [first, last) .

Original:

Reverses the order of the elements in the range [first, last).

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 -

el intervalo de elementos de revertir

Original:

the range of elements to reverse

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Requisitos de tipo
-BidirIt debe reunir los requerimientos de BidirectionalIterator.
-The type of dereferenced BidirIt must meet the requirements of Swappable.

Valor de retorno

(Ninguno)

Original:

(none)

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Posible implementación

template<class BidirIt>
void reverse(BidirIt first, BidirIt last)
{
    while ((first != last) && (first != --last)) {
        std::swap(*first++, *last);
    }
}

Ejemplo

#include <vector>
#include <iostream>
#include <algorithm>
 
int main(int argc, char** argv)
{
    std::vector<int> v({1,2,3});
    std::reverse(std::begin(v), std::end(v));
    std::cout << v[0] << v[1] << v[2] << '\n';

    int a[] = {4, 5, 6, 7};
    std::reverse(&a[0], &a[4]);
    std::cout << a[0] << a[1] << a[2] << a[3] << '\n';
}

Salida:

Complejidad

lineal en la distancia entre first y 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.

Ver también