Previous fileTop of DocumentContentsIndex pageNext file
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.

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.

Previous fileTop of DocumentContentsIndex pageNext file