怎样求交集,该如何解决
怎样求交集
vector <int> group1; // group1的内容是{ 1, 2, 3, 4, 5,...}
vector <int> group2; // group2的内容是{ 2, 3, 4, 6, 34, 22,...}
// group内容是什么不重要
问题:怎样求 group1和group2 的交集呢?
------解决方案--------------------
要是排序好的话
algorithm中有个现成的算法 set_ 忘了具体的名字,好像就4个set_开头的算法
------解决方案--------------------
set_intersection
Syntax:
#include <algorithm>
iterator set_intersection( iterator start1, iterator end1, iterator start2, iterator end2, iterator result );
iterator set_intersection( iterator start1, iterator end1, iterator start2, iterator end2, iterator result, StrictWeakOrdering cmp );
The set_intersection() algorithm computes the intersection of the two sets defined by [start1,end1) and [start2,end2) and stores the intersection starting at result.
Both of the sets, given as ranges, must be sorted in ascending order.
The return value of set_intersection() is an iterator to the end of the intersection range.
If the strict weak ordering comparison function object cmp is not specified, set_intersection() will use the < operator to compare elements.
vector <int> group1; // group1的内容是{ 1, 2, 3, 4, 5,...}
vector <int> group2; // group2的内容是{ 2, 3, 4, 6, 34, 22,...}
// group内容是什么不重要
问题:怎样求 group1和group2 的交集呢?
------解决方案--------------------
要是排序好的话
algorithm中有个现成的算法 set_ 忘了具体的名字,好像就4个set_开头的算法
------解决方案--------------------
set_intersection
Syntax:
#include <algorithm>
iterator set_intersection( iterator start1, iterator end1, iterator start2, iterator end2, iterator result );
iterator set_intersection( iterator start1, iterator end1, iterator start2, iterator end2, iterator result, StrictWeakOrdering cmp );
The set_intersection() algorithm computes the intersection of the two sets defined by [start1,end1) and [start2,end2) and stores the intersection starting at result.
Both of the sets, given as ranges, must be sorted in ascending order.
The return value of set_intersection() is an iterator to the end of the intersection range.
If the strict weak ordering comparison function object cmp is not specified, set_intersection() will use the < operator to compare elements.