How to find the set intersection of two sets using STL algorithm?
Code:set<int> s1; set<int> s2; //& want to save it in set s i.e. s = s1^s2 set<int> s; //i.e. s=?
How to find the set intersection of two sets using STL algorithm?
Code:set<int> s1; set<int> s2; //& want to save it in set s i.e. s = s1^s2 set<int> s; //i.e. s=?
Last edited by kapil1089thekin; 08-11-2010 at 03:38 PM.
Check here for an example:
set_intersection - C++ Reference
It gives the number of the element in the given example.. I want to get the intersected set.
It gives the intersected set. The example just uses it only to display its size.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
it gives the intersected set but the size of v doesn't change with the intersection size.
You're concerned that v may be too large? Then may I suggest using a back insert iterator to put elements of the intersected set at the end of the vector:
Output iterators usually start at the beginning, which will overwrite elements instead of letting the vector (or whatever) grow naturally. See back_insert_iterator - C++ ReferenceCode:#include <iterator> vector<int> v; set_intersection (first, first+5, second, second+5, back_inserter(v)); cout << "intersection has " << v.size() << " elements.\n";