thrust::set_intersection

Defined in thrust/set_operations.h

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename StrictWeakCompare>
OutputIterator thrust::set_intersection(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, StrictWeakCompare comp)

set_intersection constructs a sorted range that is the intersection of sorted ranges [first1, last1) and [first2, last2). The return value is the end of the output range.

In the simplest case, set_intersection performs the “intersection” operation from set theory: the output range contains a copy of every element that is contained in both [first1, last1) and [first2, last2). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if a value appears m times in [first1, last1) and n times in [first2, last2) (where m may be zero), then it appears min(m,n) times in the output range. set_intersection is stable, meaning that both elements are copied from the first range rather than the second, and that the relative order of elements in the output range is the same as in the first input range.

This version of set_intersection compares elements using a function object comp.

The following code snippet demonstrates how to use set_intersection to compute the set intersection of sets of integers sorted in descending order.

#include <thrust/set_operations.h>
...
int A1[6] = {11, 9, 7, 5, 3, 1};
int A2[7] = {13, 8, 5, 3, 2,  1, 1};

int result[3];

int *result_end = thrust::set_intersection(A1, A1 + 6, A2, A2 + 7, result, thrust::greater<int>());
// result is now {5, 3, 1}

See also

includes

See also

set_union

See also

set_intersection

See also

set_symmetric_difference

See also

sort

See also

is_sorted

Parameters
  • first1 – The beginning of the first input range.

  • last1 – The end of the first input range.

  • first2 – The beginning of the second input range.

  • last2 – The end of the second input range.

  • result – The beginning of the output range.

  • comp – Comparison operator.

Template Parameters
  • InputIterator1 – is a model of Input Iterator, InputIterator1 and InputIterator2 have the same value_type, InputIterator1's value_type is a model of LessThan Comparable, the ordering on InputIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator1's value_type is convertable to a type in OutputIterator's set of value_types.

  • InputIterator2 – is a model of Input Iterator, InputIterator2 and InputIterator1 have the same value_type, InputIterator2's value_type is a model of LessThan Comparable, the ordering on InputIterator2's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements, and InputIterator2's value_type is convertable to a type in OutputIterator's set of value_types.

  • OutputIterator – is a model of Output Iterator.

Returns

The end of the output range.

Pre

The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp.

Pre

The resulting range shall not overlap with either input range.