两个排序范围的并集 两个集合的并集是由其中一个集合或两个集合中的元素组成的。第二个范围中的图元如果在第一个范围中具有等效图元,则不会复制到结果范围中。 对于第一个版本,使用运算符 范围内的元件应已订购。 1. 使用默认运算符
null
Template :OutputIterator set_union (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.compBinary function that accepts two arguments of the types pointed bythe 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.
CPP
// CPP program to illustrate // std :: set_union #include <algorithm> // std::set_union, 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; std::sort(first, first + n); std::sort(second, second + n); // Using default function it = std::set_union(first, first + n, second, second + n, v.begin()); std::cout << "The union 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 union has 8 elements: 5 10 15 20 25 30 40 50
2. 使用自定义函数: 语法:
Template :OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);Parameters :first1, last1, first2, last2, result are same as above.compBinary function that accepts two arguments of the types pointed bythe 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.
CPP
// CPP program to demonstrate use of // std :: set_symmetric_difference #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; // Driver code int main() { string first[] = { "Sachin" , "Rakesh" , "Sandeep" , "Serena" }; string second[] = { "Vaibhav" , "Sandeep" , "Rakesh" , "Neha" }; int n = sizeof (first) / sizeof (first[0]); // Print students of first list cout << "Students in first subject :" ; for ( int i = 0; i < n; i++) cout << " " << first[i]; cout << "" ; // Print students of second list cout << "Students in second subject :" ; for ( int i = 0; i < n; i++) cout << " " << second[i]; cout << "" ; vector<string> v(10); vector<string>::iterator it, st; // Sorting both the list sort(first, first + n); sort(second, second + n); // Using default operator< it = set_union(first, first + n, second, second + n, v.begin()); cout << "Students attending both subjects are :" ; for (st = v.begin(); st != it; ++st) cout << ' ' << *st; cout << '' ; return 0; } |
输出:
Students in first subject : Sachin Rakesh Sandeep SerenaStudents in second subject : Vaibhav Sandeep Rakesh NehaStudents attending both subjects are :Neha Rakesh Sachin Sandeep Serena Vaibhav
可能的应用: 它用于查找存在于一个容器或两个容器中的元素。 1.可以用来 找到所有参加这两门课程的学生的名单。
CPP
// CPP program to demonstrate use of // std :: set_symmetric_difference #include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; // Driver code int main() { string first[] = { "John" , "Bob" , "Mary" , "Serena" }; string second[] = { "Jim" , "Mary" , "John" , "Bob" }; int n = sizeof (first) / sizeof (first[0]); // Print students of first list cout << "Students in first subject :" ; for ( int i = 0; i < n; i++) cout << " " << first[i]; cout << "" ; // Print students of second list cout << "Students in second subject :" ; for ( int i = 0; i < n; i++) cout << " " << second[i]; cout << "" ; vector<string> v(10); vector<string>::iterator it, st; // Sorting both the list sort(first, first + n); sort(second, second + n); // Using default operator< it = set_union(first, first + n, second, second + n, v.begin()); cout << "Students attending both subjects are :" ; for (st = v.begin(); st != it; ++st) cout << ' ' << *st; cout << '' ; return 0; } |
输出:
Students in first subject : Sachin Rakesh Sandeep SerenaStudents in second subject : Vaibhav Sandeep Rakesh NehaStudents attending both subjects are :Neha Rakesh Sachin Sandeep Serena Vaibhav
2.也可以用来求两个集合的并集。 程序如上所示。 本文由 萨钦·比什特 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END