Skip to the content of the web site.

Merge and set algorithms

These functions are found in the #include <algorithm> library.

All but one of these functions take as their arguments five iterators where the first four iterators are such that first two and second two both define sorted ranges another fifth iterator is an output iterator referring to the first location where the result of the operation should be placed. The returned iterator always defines right-hand side of the range to which the output was copied. The last function is Boolean-valued and only takes the first four arguments, and determines if the second sorted range is contained in the first sorted range.

The merge operation allows for duplicate entries, while the set operations are ideal under the assume uniqueness. A sorted range that is not a unique can be converted into one using the std::unique(...) function.

These set operations can be described visually by looking at the images in Figure 1, where Set A is the first range and Set B is the second range. Those entries that appear in both ranges are represented by the overlapping region of the two circles where

  • the overlapping region represents any entries both in both ranges (both Sets A and B),
  • the left crescent represents any entries in the first range but not in the second (so only in Set A), and
  • the right crescent represents any entries in the second range, but not in the first (so only in Set B).

The entries in the first and second ranges are shown in first pair of images. The output of the four operations appear in the next two pairs of images with those entries appearing in the output shown in red, and the last pair demonstrate under which conditions the first range contains the second (that is, when every entry in the second range is also in the first).


Figure 1. The sets A and B together with visual representations of the union, the intersection and the symmetric difference as well as the difference of A minus B.

All of these functions allow for a sixth optional argument that is of type bool predicate( T lhs, T rhs ) that returns true if the lhs argument is less than the rhs.

itr_t std::merge( itr_t itr_1_first, itr_t itr_1_last, itr_t itr_2_first, itr_t itr_2_last, itr_t itr_out_first )

The two sorted ranges are merged into one, with all entries that are equal from the first range being copied to the output first in order (and thus, stable), followed by all from the second range also in order (thus, also stable). The size of the output range must equal the sum of the sizes of the input ranges.

itr_t std::set_union( itr_t itr_1_first, itr_t itr_1_last, itr_t itr_2_first, itr_t itr_2_last, itr_t itr_out_first )

If the two sorted ranges are also unique, this combines the two into a single sorted-and-unique range where if there are equal entries, the entry in the first range is the one copied over to the output range.

If there are multiple equal entries in each set, then if for a given value there are m equal entries in the first range and n equal entries in the second, then std::max(m, n) entries are copied to the output range.

itr_t std::set_intersection( itr_t itr_1_first, itr_t itr_1_last, itr_t itr_2_first, itr_t itr_2_last, itr_t itr_out_first )

If the two sorted ranges are also unique, this moves to the output range only those entries in the first range for which there is an equal entry in the second range.

If there are multiple equal entries in each set, then if for a given value there are m equal entries in the first range and n equal entries in the second, then std::min(m, n) entries are copied to the output range.

itr_t std::set_difference( itr_t itr_1_first, itr_t itr_1_last, itr_t itr_2_first, itr_t itr_2_last, itr_t itr_out_first )

If the two sorted ranges are also unique, this moves to the output range only those entries in the first range that do not appear in the second range.

If there are multiple equal entries in each set, then if for a given value there are m equal entries in the first range and n equal entries in the second, then std::max(m - n, 0) entries are copied to the output range.

itr_t std::set_symmetric_difference( itr_t itr_1_first, itr_t itr_1_last, itr_t itr_2_first, itr_t itr_2_last, itr_t itr_out_first )

If the two sorted ranges are also unique, this moves to the output range only those entries that appear in only one of the two ranges.

If there are multiple equal entries in each set, then if for a given value there are m equal entries in the first range and n equal entries in the second, then std::abs(m - n) entries are copied to the output range.

bool std::contains( itr_t itr_1_first, itr_t itr_1_last, itr_t itr_2_first, itr_t itr_2_last )

If the two sorted ranges are also unique, this returns true if each entry in the second range appears in the first; that is the first range contains the second range. It can also be described as returning true if the second range is a "subset" of the first.

If there are multiple equal entries in each set, then if for a given value there are m equal entries in the first range and n equal entries in the second, then for the second to be a subset of the first, m >= n.