set_difference


Create a set of elements in the first sequence that are not in the second.

Place the sorted difference of all the elements in the sequences [ first1...last1 ) and [ first2...last2 ) into a sequence starting at result . The output sequence contains all elements that are in the first sequence but that are not in the second sequence. Return an iterator positioned immediately after the end of the new sequence. The result is undefined if the two input sequences overlap. The first version assumes the elements are sorted using operator< , whereas the second version assumes the elements are sorted using the binary function compare .

Library

Standards<ToolKit>

Declaration


#include <algorithm>

template
  <
  class InputIterator1,
  class InputIterator2,
  class OutputIterator
  >
OutputIterator set_difference
  (
  InputIterator1 first1,
  InputIterator1 last1,
  InputIterator2 first2,
  InputIterator2 last2,
  OutputIterator result
  );

template
  <
  class InputIterator1,
  class InputIterator2,
  class OutputIterator,
  class Compare
  >
OutputIterator set_difference
  (
  InputIterator1 first1,
  InputIterator1 last1,
  InputIterator2 first2,
  InputIterator2 last2,
  OutputIterator result,
  Compare compare
  );
  

Complexity

Time complexity is linear. Space complexity is constant.

Example <ospace/osstd/examples/setdiff0.cpp>
#include <iostream>
#include <algorithm>

int v1[ 3 ] = { 13, 18, 23 };
int v2[ 4 ] = { 10, 13, 17, 23 };
int result[ 4 ] = { 0, 0, 0, 0 };

void
main()
  {
  set_difference
    (
    v1,
    v1 + 3,
    v2,
    v2 + 4,
    result
    );

  int i;
  for ( i = 0; i < 4; ++i )
    cout << result[ i ] << ` `;
  cout << "\n";

  set_difference
    (
    v2,
    v2 + 4,
    v1,
    v1 + 2,
    result
    );

  for ( i = 0; i < 4; ++i )
    cout << result[ i ] << ` `;
  cout << "\n";
  }

18 0 0 0
10 17 23 0
Example <ospace/osstd/examples/setdiff1.cpp>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>

void
main()
  {
  vector< int > v1( 10 );
  vector< int > v2( 10 );
  for ( int i = 0; i < v1.size(); ++i )
    {
    v1[ i ] = i;
    v2[ i ] = 7 + i;
    }
  ostream_iterator< int > iter( cout, " " );
  cout << "v1: ";
  copy( v1.begin(), v1.end(), iter );
  cout << "\nv2: ";
  copy( v2.begin(), v2.end(), iter );
  cout << "\n";

  set_difference( v1.begin(), v1.end(), v2.begin(), v2.end(), iter );

  cout << "\n";
  }

v1: 0 1 2 3 4 5 6 7 8 9
v2: 7 8 9 10 11 12 13 14 15 16
0 1 2 3 4 5 6
Example <ospace/osstd/examples/setdiff2.cpp>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <functional>
#include <iterator>

char* word1 = "ABCDEFGHIJKLMNO";
char* word2 = "LMNOPQRSTUVWXYZ";

void
main()
  {
  ostream_iterator< char > iter( cout, " " );
  cout << "word1: ";
  copy( word1, word1 + ::strlen( word1 ), iter );
  cout << "\nword2: ";
  copy( word2, word2 + ::strlen( word2 ), iter );
  cout << "\n";

  set_difference
    (
    word1,
    word1 + ::strlen( word1 ),
    word2,
    word2 + ::strlen( word2 ),
    iter,
    less< char >()
    );

  cout << "\n";
  }

word1: A B C D E F G H I J K L M N O
word2: L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.