两个集合的交集仅由两个集合中存在的元素构成。函数复制的元素总是来自第一个范围,顺序相同。两个范围内的元件应已订购。 例如:
null
Input :5 10 15 20 2550 40 30 20 10Output :The intersection has 2 elements :10 20
1. 使用“ 语法:
Template :OutputIterator set_intersection (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 first range.Return Type :An iterator to the end of the constructed range.
CPP
// CPP program to illustrate // std :: set_intersection #include <bits/stdc++.h> int main() { int first[] = { 5, 10, 15, 20, 25 }; int second[] = { 50, 40, 30, 20, 10 }; int n = sizeof (first) / sizeof (first[0]); std::vector< int > v1(5); std::vector< int > v2(5); std::vector< int >::iterator it, ls; std::sort(first, first + 5); std::sort(second, second + 5); // Print elements std::cout << "First array :"; for ( int i = 0; i < n; i++) std::cout << " " << first[i]; std::cout << ""; // Print elements std::cout << "Second array :"; for ( int i = 0; i < n; i++) std::cout << " " << second[i]; std::cout << ""; // std :: set_intersection ls = std::set_intersection(first, first + 5, second, second + 5, v1.begin()); std::cout << "The intersection has " << (ls - v1.begin()) << " elements:"; for (it = v1.begin(); it != ls; ++it) std::cout << ' ' << *it; std::cout << ""; return 0; } |
输出:
First array : 5 10 15 20 25Second array : 10 20 30 40 50The intersection has 2 elements: 10 20
2. 通过使用预定义函数进行比较: 语法:
Template :OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);Parameters :first1, last1, first2, last2, result are same as mentioned 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.It follows the strict weak ordering to order the elements.Return Type :An iterator to the end of the constructed range.
CPP
// CPP program to illustrate // std :: set_intersection #include <bits/stdc++.h> bool comp( int a, int b) { return a < b; } int main() { int first[] = { 5, 10, 15, 20, 25 }; int second[] = { 50, 40, 30, 20, 10 }; int n = sizeof (first) / sizeof (first[0]); std::vector< int > v1(5); std::vector< int > v2(5); std::vector< int >::iterator it, ls; std::sort(first, first + 5); std::sort(second, second + 5); // Print elements std::cout << "First array :"; for ( int i = 0; i < n; i++) std::cout << " " << first[i]; std::cout << ""; // Print elements std::cout << "Second array :"; for ( int i = 0; i < n; i++) std::cout << " " << second[i]; std::cout << ""; // std :: set_intersection ls = std::set_intersection(first, first + 5, second, second + 5, v1.begin(), comp); std::cout << "The intersection has " << (ls - v1.begin()) << " elements:"; for (it = v1.begin(); it != ls; ++it) std::cout << ' ' << *it; std::cout << ""; return 0; } |
输出:
First array : 5 10 15 20 25Second array : 10 20 30 40 50The intersection has 2 elements: 10 20
可能的应用: 它用于查找仅存在于两个集合中的元素。 1.可以用来 找出两个班级的学生名单。
CPP
// CPP program to demonstrate use of // std :: set_intersection #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 class :"; for ( int i = 0; i < n; i++) cout << " " << first[i]; cout << ""; // Print students of second list cout << "Students in second class :"; 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_intersection(first, first + n, second, second + n, v.begin()); cout << "Students attending both the classes only are :"; for (st = v.begin(); st != it; ++st) cout << ' ' << *st; cout << '' ; return 0; } |
输出:
Students in first class : Sachin Rakesh Sandeep SerenaStudents in second class : Vaibhav Sandeep Rakesh NehaStudents attending both classes only are : Rakesh Sandeep
2.它也可以用来 在这两个列表中找到共同的元素。 程序如上所示。 3.复杂性 复杂度在[first1,last1]和[first2,last2]之间呈线性关系:执行多达 2*(count1+count2)-1比较。其中count1=last1-first1和count2=last2-first2。 本文由 萨钦·比什特 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END