function template
<algorithm>

# std::set_intersection

default (1) ```template OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); ``` ```template 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)`.

The intersection of two sets is formed only by the elements that are present in both sets. The elements copied by the function come always from the first range, in the same order.

The elements are compared using `operator<` for the first version, and comp for the second. Two elements, a and b are considered equivalent if `(!(a<b) && !(b<a))` or if `(!comp(a,b) && !comp(b,a))`.

The elements in the ranges shall already be ordered according to this same criterion (`operator<` or comp). The resulting range is also sorted according to this.

The behavior of this function template is equivalent to:
 ``12345678910111213141516`` ``````template 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; ++result; ++first1; ++first2; } } return result; }``````

### Parameters

first1, last1
Input iterators to the initial and final positions of the first sorted 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 sorted sequence. The range used is `[first2,last2)`.
result
Output iterator to the initial position of the range where the resulting sequence is stored.
The pointed type shall support being assigned the value of an element from the first range.
comp
Binary function that accepts two arguments of the types pointed by the input iterators, and returns a value convertible to `bool`. The value returned indicates whether the first argument is considered to go before the second in the specific strict weak ordering it defines.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.

The ranges shall not overlap.

### Return value

An iterator to the end of the constructed range.

### Example

 ``12345678910111213141516171819202122232425`` ``````// set_intersection example #include // std::cout #include // std::set_intersection, std::sort #include // std::vector int main () { int first[] = {5,10,15,20,25}; int second[] = {50,40,30,20,10}; std::vector v(10); // 0 0 0 0 0 0 0 0 0 0 std::vector::iterator it; std::sort (first,first+5); // 5 10 15 20 25 std::sort (second,second+5); // 10 20 30 40 50 it=std::set_intersection (first, first+5, second, second+5, v.begin()); // 10 20 0 0 0 0 0 0 0 0 v.resize(it-v.begin()); // 10 20 std::cout << "The intersection has " << (v.size()) << " elements:\n"; for (it=v.begin(); it!=v.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }``````

Output:
 ```The intersection has 2 elements: 10 20 ```

### Complexity

Up to linear in `2*(count1+count2)-1` (where countX is the distance between firstX and lastX): Compares and assigns elements.

### Data races

The objects in the ranges `[first1,last1)` and `[first2,last2)`are accessed.
The objects in the range between result and the returned value are modified.

### Exceptions

Throws if any of the element comparisons, the element assignments or the operations on iterators throws.
Note that invalid arguments cause undefined behavior.