Diferencia simétrica de dos rangos ordenados
La diferencia simétrica entre dos conjuntos está formada por los elementos que están presentes en uno de los conjuntos, pero no en el otro. Entre los elementos equivalentes en cada rango, se descartan los que aparecen antes en el orden existente antes de la convocatoria. El orden existente también se conserva para los elementos copiados.
Los elementos se comparan usando operator< para la primera versión y comp para la segunda. Dos elementos, a y b, se consideran equivalentes si (!(a<b) && !(b<a)) o si (!comp(a, b) && !comp(b, a)).
Los elementos de las gamas ya estarán ordenados.
1. Uso del operador predeterminado<:
Sintaxis:
Template : OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); Parameters : first1, last1 Input iterators to the initial and final positions of the first sorted sequence. The range used is [first1, last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1. first2, last2 Input iterators to the initial and final positions of the second sorted sequence. The range used is [first2, last2). result Output iterator to the initial position of the range where the resulting sequence is stored. The pointed type shall support being assigned the value of an element from the other ranges. comp Binary function that accepts two arguments of the types pointed by the input iterators, and returns a value convertible to bool. The function shall not modify any of its arguments. This can either be a function pointer or a function object. The ranges shall not overlap. Return Type : An iterator to the end of the constructed range.
CPP
// CPP program to illustrate // std :: set_symmetric_difference #include <algorithm> // std::set_symmetric_difference, std::sort #include <iostream> // std::cout #include <vector> // std::vector // Driver code int main() { int first[] = { 5, 10, 15, 20, 25 }; int second[] = { 50, 40, 30, 20, 10 }; int n = sizeof(first) / sizeof(first[0]); // Print first array std::cout << "First array contains :"; for (int i = 0; i < n; i++) std::cout << " " << first[i]; std::cout << "\n"; // Print second array std::cout << "Second array contains :"; for (int i = 0; i < n; i++) std::cout << " " << second[i]; std::cout << "\n\n"; std::vector<int> v(10); std::vector<int>::iterator it, st; // Sorting both the arrays std::sort(first, first + 5); std::sort(second, second + 5); // Using default operator< it = std::set_symmetric_difference(first, first + 5, second, second + 5, v.begin()); std::cout << "The symmetric difference has " << (it - v.begin()) << " elements:\n"; for (st = v.begin(); st != it; ++st) std::cout << ' ' << *st; std::cout << '\n'; return 0; }
Producción:
First array contains : 5 10 15 20 25 Second array contains : 50 40 30 20 10 The symmetric difference has 6 elements: 5 15 25 30 40 50
2. Usando la función personalizada:
Sintaxis:
Template : OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); Parameters : first1, last1, first2, last2, result are same as described above. comp Binary function that accepts two arguments of the types pointed by the input iterators, and returns a value convertible to bool. The function shall not modify any of its arguments. This can either be a function pointer or a function object. The ranges shall not overlap. Return Type : An iterator to the end of the constructed range.
CPP
// CPP program to illustrate // std :: set_symmetric_difference #include <algorithm> // std::set_symmetric_difference, std::sort #include <iostream> // std::cout #include <vector> // std::vector // Custom function bool comp(int a, int b) { return a < b; } // Driver code int main() { int first[] = { 5, 10, 15, 20, 25 }; int second[] = { 50, 40, 30, 20, 10 }; int n = sizeof(first) / sizeof(first[0]); // Print first array std::cout << "First array contains :"; for (int i = 0; i < n; i++) std::cout << " " << first[i]; std::cout << "\n"; // Print second array std::cout << "Second array contains :"; for (int i = 0; i < n; i++) std::cout << " " << second[i]; std::cout << "\n\n"; std::vector<int> v(10); std::vector<int>::iterator it, st; // Sorting both the arrays std::sort(first, first + 5); std::sort(second, second + 5); // Using default operator< it = std::set_symmetric_difference(first, first + 5, second, second + 5, v.begin(), comp); std::cout << "The symmetric difference has " << (it - v.begin()) << " elements:\n"; for (st = v.begin(); st != it; ++st) std::cout << ' ' << *st; std::cout << '\n'; return 0; }
Producción:
First array contains : 5 10 15 20 25 Second array contains : 50 40 30 20 10 The symmetric difference has 6 elements: 5 15 25 30 40 50
Posible Aplicación: Se utiliza para encontrar los elementos que están presentes en un contenedor y no en otro contenedor.
1. Se utiliza para encontrar la lista de estudiantes que no están tomando ambas clases. Los estudiantes de ambas clases están presentes en las listas.
CPP
// CPP program to illustrate // std :: set_symmetric_difference #include <bits/stdc++.h> using namespace std; int main() { // students attending first class std::vector<string> class1{ "Samir", "Manoj", "Pranav", "Rajesh" }; // students attending second class std::vector<string> class2{ "Samir", "Junaid", "Manoj", "Pankaj", "Arpit" }; cout << "Students attending first class are : "; for (auto i : class1) { cout << i << " "; } cout << "\nStudents attending second class are : "; for (auto i : class2) { cout << i << " "; } // to store the result of symmetric difference std::vector<string> result(10); std::vector<string>::iterator it; // finding symmetric difference it = set_symmetric_difference(class1.begin(), class1.end(), class2.begin(), class2.end(), result.begin()); cout << "\n\nList of students that are not taking both classes :"; for (std::vector<string>::iterator i = result.begin(); i != it; i++) { cout << *i << " "; } return 0; }
PRODUCCIÓN :
Students attending first class are : Samir Manoj Pranav Rajesh Students attending second class are : Samir Junaid Manoj Pankaj Arpit List of students that are not taking both classes :Junaid Pankaj Arpit Pranav Rajesh
2. También se puede usar para encontrar los números de ambas listas que no están presentes en ambas listas.
El programa se da arriba.
Este artículo es una contribución de Sachin Bisht . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA