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=?
Printable View
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=?
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.
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";