set_difference [C++ Reference]
Keywords: stl,algorithm,set_difference
Date: 2010-04-07T05:30:49-0700

## set_difference

Syntax:

        #include <algorithm>
        template< typename InIterA, typename InIterB, typename OutIter >
        OutIter set_difference( InIterA start1, InIterA end1, InIterB start2, InIterB end2, OutIter result );
        template< typename InIterA, typename InIterB, typename OutIter, typename StrictWeakOrdering >
        OutIter set_difference( InIterA start1, InIterA end1, InIterB start2, InIterB end2, OutIter result, StrictWeakOrdering cmp );

The set_difference() algorithm computes the difference between two sets defined by [start1,end1) and [start2,end2) and stores the difference starting at result.

Both of the sets, given as ranges, must be sorted in ascending order.

The return value of set_difference() is an iterator to the end of the result range.

If the strict weak ordering comparison function object cmp is not specified, set_difference() will use the < operator to compare elements.

Example

    // set_difference 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_difference (first, first+5, second, second+5, v.begin());
                                                   // 5 15 25  0  0  0  0  0  0  0

      cout << "difference has " << int(it - v.begin()) << " elements.\n";

      return 0;
    }

Output: difference has 3 elements

Related Topics: includes, set_intersection, set_symmetric_difference, set_union