Apache C++ Standard Library User's Guide

## 14.6 set Operations

The operations of `set_union`, `set_intersection`, and `set_difference` were all described in Section 8.2.7 when we discussed the set container class. However, the algorithms that implement these operations are generic, and applicable to any ordered data structure. The algorithms assume the input ranges are ordered collections that represent multisets; that is, elements can be repeated. However, if the inputs represent sets, then the result will always be a set. Unlike the `std::merge()` algorithm, none of the set algorithms produce repeated elements in the output that are not present in the input sets.

The set operations all have the same format. The two input sets are specified by pairs of input iterators. The output set is specified by an input iterator, and the end of this range is returned as the result value. An optional comparison operator is the final argument. In all cases it is required that the output sequence not overlap in any manner with either of the input sequences.

    namespace std {
      OutputIterator set_union
         (InputIterator first1, InputIterator last1,
          InputIterator first2, InputIterator last2,
          OutputIterator result [, Compare ] );
    }

The example program illustrates the use of the four set algorithms, `std::set_union()`, `std::set_intersection()`, `std::set_difference(),` and `std::set_symmetric_difference()`. It also shows a call on `std::merge()` in order to contrast the merge and the set union operations. The algorithm `std::includes()` is slightly different. Again the two input sets are specified by pairs of input iterators, and the comparison operator is an optional fifth argument. The return value for the algorithm is `true` if the first set is entirely included in the second, and `false` otherwise.

    void set_example()
    // illustrates the use of the generic set algorithms
    // see alg7.cpp for complete source code
    {
      std::ostream_iterator<int> intOut(cout, " ");

      // make a couple of ordered lists
      std::list<int> listOne, listTwo;
      std::generate_n(std::inserter(listOne, listOne.begin()), 5,
                                    iotaGen(1));
      std::generate_n(std::inserter(listTwo, listTwo.begin()), 5,
                                    iotaGen(3));

      // now do the set operations
      // union - 1 2 3 4 5 6 7
      std::set_union(listOne.begin(), listOne.end(),
                     listTwo.begin(), listTwo.end(), intOut);
      std::cout << std::endl;

      // merge - 1 2 3 3 4 4 5 5 6 7
      std::merge(listOne.begin(), listOne.end(),
                 listTwo.begin(), listTwo.end(), intOut);
      std::cout << std::endl;

      // intersection - 3 4 5
      std::set_intersection(listOne.begin(), listOne.end(),
                            listTwo.begin(), listTwo.end(), intOut);
      std::cout << std::endl;

      // difference - 1 2
      std::set_difference(listOne.begin(), listOne.end(),
                          listTwo.begin(), listTwo.end(), intOut);
      std::cout << std::endl;
      // symmetric difference - 1 2 6 7
      std::set_symmetric_difference(listOne.begin(), listOne.end(),
                                    listTwo.begin(), listTwo.end(),
                                    intOut);
      std::cout << std::endl;

      if (std::includes(listOne.begin(), listOne.end(),
                        listTwo.begin(), listTwo.end()))
        std::cout << "set is subset" << std::endl;
      else
        std::cout << "set is not subset" << std::endl;
    }