◐ Shell
clean mode source ↗

std::is_permutation - 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 ForwardIt1, class ForwardIt2 > bool is_permutation( ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first );

(1) (desde C++11)

template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > bool is_permutation( ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first, BinaryPredicate p );

(2) (desde C++11)

Regreso true si existe una permutación de los elementos en el rango [first1, last1) que hace que rango igual al rango de principio a d_first. La primera versión utiliza operator== por la igualdad, la segunda versión utiliza el p predicado binario

Original:

Returns true if there exists a permutation of the elements in the range [first1, last1) that makes that range equal to the range beginning at d_first. The first version uses operator== for equality, the second version uses the binary predicate p

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 para comparar

Original:

the range of elements to compare

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

d_first -

el comienzo de la segunda gama de comparar

Original:

the beginning of the second range to compare

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

p - Predicado binario que devuelve ​true si los elementos deben tratarse como iguales.

La signatura de la función predicado deberá ser equivalente a la siguiente:

bool pred(const Tipo1 &a, const Tipo2 &b);

Mientras que la signatura no necesita tener const &, la función no debe modificar los objetos que se le han pasado y debe ser capaz de aceptar todos los valores de tipo (posiblemente const) Tipo1 y Tipo2 independientemente de la categoría de valor (por lo tanto, no se permite Tipo1 &, ni Tipo1 a menos que para Tipo1 un movimiento sea equivalente a una copia (desde C++11)).
Los tipos Tipo1 y Tipo2 deben ser tales que objetos de tipo ForwardIt1 y ForwardIt2 pueden ser desreferenciados y luego convertidos implícitamente a Tipo1 and Tipo2 respectively. ​

Requisitos de tipo
-ForwardIt1, ForwardIt2 debe reunir los requerimientos de ForwardIterator.

Valor de retorno

true si la [first, last) rango es una permutación del rango de principio a d_first .

Original:

true if the range [first, last) is a permutation of the range beginning at d_first.

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

Complejidad

En la mayoría de las aplicaciones O(N2) del predicado, o exactamente N si las secuencias son ya iguales, N=std::distance(first, last) donde .

Original:

At most O(N2) applications of the predicate, or exactly N if the sequences are already equal, where N=std::distance(first, last).

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 ForwardIt1, class ForwardIt2>
bool is_permutation(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 d_first)
{
   // skip common prefix
   std::tie(first, d_first) = std::mismatch(first, last, d_first);
   // iterate over the rest, counting how many times each element
   // from [first, last) appears in [d_first, d_last)
   if (first != last) {
       ForwardIt2 d_last = d_first;
       std::advance(d_last, std::distance(first, last));
       for (ForwardIt1 i = first; i != last; ++i) {
            if (i != std::find(first, i, *i)) continue; // already counted this *i

            auto m = std::count(d_first, d_last, *i);
            if (m==0 || std::count(i, last, *i) != m) {
                return false;
            }
        }
    }
    return true;
}

Ejemplo

#include <algorithm>
#include <vector>
#include <iostream>
int main()
{
    std::vector<int> v1{1,2,3,4,5};
    std::vector<int> v2{3,5,4,1,2};
    std::cout << "3,5,4,1,2 is a permutation of 1,2,3,4,5? "
              << std::boolalpha
              << std::is_permutation(v1.begin(), v1.end(), v2.begin()) << '\n';

    std::vector<int> v3{3,5,4,1,1};
    std::cout << "3,5,4,1,1 is a permutation of 1,2,3,4,5? "
              << std::boolalpha
              << std::is_permutation(v1.begin(), v1.end(), v3.begin()) << '\n';
}

Salida:

3,5,4,1,2 is a permutation of 1,2,3,4,5? true
3,5,4,1,1 is a permutation of 1,2,3,4,5? false

Ver también

Genera la siguiente permutación lexicográfica mayor de un rango de elementos.
(plantilla de función) [editar]
genera la siguiente permutación lexicográfica menor de un rango de elementos.
(plantilla de función) [editar]