两个排序区间的对称差 两个集合之间的对称差异是由其中一个集合中存在的元素构成的,而另一个集合中不存在。在每个范围中的等效元素中,被丢弃的是那些在调用之前以存在顺序出现的元素。复制的图元也会保留现有的顺序。 对于第一个版本,使用运算符 范围内的元件应已订购。 1. 使用默认运算符 语法:
null
Template :OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result);Parameters :first1, last1Input iterators to the initial and final positions of the firstsorted sequence. The range used is [first1, last1), which containsall the elements between first1 and last1, including the elementpointed by first1 but not the element pointed by last1.first2, last2Input iterators to the initial and final positions of the secondsorted sequence. The range used is [first2, last2).resultOutput iterator to the initial position of the range where theresulting sequence is stored.The pointed type shall support being assigned the value of anelement from the other ranges.compBinary function that accepts two arguments of the types pointedby 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 << "" ; // Print second array std::cout << "Second array contains :" ; for ( int i = 0; i < n; i++) std::cout << " " << second[i]; std::cout << "" ; 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:" ; for (st = v.begin(); st != it; ++st) std::cout << ' ' << *st; std::cout << '' ; return 0; } |
输出:
First array contains : 5 10 15 20 25Second array contains : 50 40 30 20 10The symmetric difference has 6 elements: 5 15 25 30 40 50
2. 使用自定义函数: 语法:
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.compBinary function that accepts two arguments of the types pointedby 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 << "" ; // Print second array std::cout << "Second array contains :" ; for ( int i = 0; i < n; i++) std::cout << " " << second[i]; std::cout << "" ; 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:" ; for (st = v.begin(); st != it; ++st) std::cout << ' ' << *st; std::cout << '' ; return 0; } |
输出:
First array contains : 5 10 15 20 25Second array contains : 50 40 30 20 10The symmetric difference has 6 elements: 5 15 25 30 40 50
可能的应用: 它用于查找存在于一个容器中而不存在于另一个容器中的元素。 1.用于查找 不同时上这两门课的学生名单。 两个班的学生都在名单上。
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 << "Students 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 << "List of students that are not taking both classes :" ; for (std::vector<string>::iterator i = result.begin(); i != it; i++) { cout << *i << " " ; } return 0; } |
输出:
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.它也可以用来 从两个列表中查找不在两个列表中的数字。 上面给出了程序。 本文由 萨钦·比什特 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END