|
set_intersection
function template
<algorithm>
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result );
template <class InputIterator1, class InputIterator2,
class OutputIterator, class Compare>
OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp );
Intersection of two sorted ranges
Constructs a sorted range beginning in the location pointed by result with the set intersection of the two sorted ranges [first1,last1) and [first2,last2) as content.
The intersection of two sets is formed only by the elements that are present in both sets at the same time.
The comparison to check for equivalence of values, uses either operator< for the first version, or comp for the second, in order to test this; The value of an element, a, is equivalent to another one, b, when (!a<b && !b<a) or (!comp(a,b) && !comp(b,a)).
For the function to yield the expected result, the elements in the ranges shall be already ordered according to the same strict weak ordering criterion (operator< or comp).
The behavior of this function template is equivalent to:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result )
{
while (first1!=last1 && first2!=last2)
{
if (*first1<*first2) ++first1;
else if (*first2<*first1) ++first2;
else { *result++ = *first1++; first2++; }
}
return result;
}
|
Parameters
- first1, last1
- Input iterators to the initial and final positions of the first sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.
- first2, last2
- Input iterators to the initial and final positions of the second sequence. The range used is [first2,last2).
- comp
- Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.
Return value
An iterator to the end of the constructed range.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
// set_intersection example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0
vector<int>::iterator it;
sort (first,first+5); // 5 10 15 20 25
sort (second,second+5); // 10 20 30 40 50
it=set_intersection (first, first+5, second, second+5, v.begin());
// 10 20 0 0 0 0 0 0 0 0
cout << "intersection has " << int(it - v.begin()) << " elements.\n";
return 0;
}
|
Output:
intersection has 2 elements
|
Complexity
At most, performs 2*(count1+count2)-1 comparisons or applications of comp (where countX is the distance between firstX and lastX).
See also
set_union | Union of two sorted ranges (function template) |
merge | Merge sorted ranges (function template) |
|