Template Function thrust::set_difference(InputIterator1, InputIterator1, InputIterator2, InputIterator2, OutputIterator)

Function Documentation

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

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

In the simplest case, set_difference performs the “difference” operation from set theory: the output range contains a copy of every element that is contained in [first1, last1) and not contained in [first2, last1). The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if [first1, last1) contains m elements that are equivalent to each other and if [first2, last2) contains n elements that are equivalent to them, the last max(m-n,0) elements from [first1, last1) range shall be copied to the output range.

This version of set_difference compares elements using operator<.

The following code snippet demonstrates how to use

set_difference to compute the set difference of two sets of integers sorted in ascending order.
Return

The end of the output range.

Pre

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

Pre

The resulting range shall not overlap with either input range.

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.

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.

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

int result[3];

int *result_end = thrust::set_difference(A1, A1 + 6, A2, A2 + 5, result);
// result is now {0, 4, 6}

See

http://www.sgi.com/tech/stl/set_difference.html

See

includes

See

set_union

See

set_intersection

See

set_symmetric_difference

See

sort

See

is_sorted