Para ver el artículo en inglés, active la casilla Inglés. También puede ver el texto en inglés en una ventana emergente si pasa el puntero del mouse por el texto.
|
Traducción
Inglés
|
is_permutation
template<class FwdIt1, class FwdIt2> bool is_permutation(FwdIt First1, FwdIt Last1, FwdIt First2); template<class FwdIt1, class FwdIt2, class Pr> bool is_permutation(FwdIt First1, FwdIt Last1, FwdIt First2, Pr Pred); // C++14 template<class FwdIt1, class FwdIt2> bool is_permutation(FwdIt First1, FwdIt Last1, FwdIt First2, FwdIt Last2); template<class FwdIt1, class FwdIt2, class Pr> bool is_permutation(FwdIt First1, FwdIt Last1, FwdIt First2, FwdIt Last2, Pr Pred);
- First1
Iterador hacia delante que hace referencia al primer elemento del intervalo. - Last1
Iterador hacia delante que hace referencia a un elemento más allá del último elemento del intervalo. - First2
Iterador hacia delante que hace referencia al primer elemento de un segundo intervalo, usado para la comparación. - Last2
Iterador hacia delante que hace referencia a un elemento más allá del último elemento de un segundo intervalo, usado para la comparación. - Pred
Predicado que comprueba la equivalencia y devuelve bool.
Ejemplo
#include <vector> #include <iostream> #include <algorithm> #include <string> using namespace std; int main() { vector<int> vec_1{ 2, 3, 0, 1, 4, 5 }; vector<int> vec_2{ 5, 4, 0, 3, 1, 2 }; vector<int> vec_3{ 4, 9, 13, 3, 6, 5 }; vector<int> vec_4{ 7, 4, 11, 9, 2, 1 }; cout << "(1) Compare using built-in == operator: "; cout << boolalpha << is_permutation(vec_1.begin(), vec_1.end(), vec_2.begin(), vec_2.end()) << endl; // true cout << "(2) Compare after modifying vec_2: "; vec_2[0] = 6; cout << is_permutation(vec_1.begin(), vec_1.end(), vec_2.begin(), vec_2.end()) << endl; // false // Define equivalence as "both are odd or both are even" cout << "(3) vec_3 is a permutation of vec_4: "; cout << is_permutation(vec_3.begin(), vec_3.end(), vec_4.begin(), vec_4.end(), [](int lhs, int rhs) { return lhs % 2 == rhs % 2; }) << endl; // true // Initialize a vector using the 's' string literal to specify a std::string vector<string> animals_1{ "dog"s, "cat"s, "bird"s, "monkey"s }; vector<string> animals_2{ "donkey"s, "bird"s, "meerkat"s, "cat"s }; // Define equivalence as "first letters are equal": bool is_perm = is_permutation(animals_1.begin(), animals_1.end(), animals_2.begin(), animals_2.end(), [](const string& lhs, const string& rhs) { return lhs[0] == rhs[0]; //std::string guaranteed to have at least a null terminator }); cout << "animals_2 is a permutation of animals_1: " << is_perm << endl; // true cout << "Press a letter" << endl; char c; cin >> c; return 0; }