libstdc++: Set Operations

libstdc++

Set Operations
Algorithms » Sorting
Collaboration diagram for Set Operations:

## Functions
template<typename _InputIterator1, typename _InputIterator2>
constexpr bool | std::includes (_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
template<typename _InputIterator1, typename _InputIterator2, typename _Compare>
constexpr bool | std::includes (_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
constexpr _OutputIterator | std::set_difference (_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare>
constexpr _OutputIterator | std::set_difference (_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
constexpr _OutputIterator | std::set_intersection (_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare>
constexpr _OutputIterator | std::set_intersection (_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
constexpr _OutputIterator | std::set_symmetric_difference (_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare>
constexpr _OutputIterator | std::set_symmetric_difference (_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
constexpr _OutputIterator | std::set_union (_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare>
constexpr _OutputIterator | std::set_union (_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)

## Detailed Description

These algorithms are common set operations performed on sequences that are already sorted. The number of comparisons will be linear.

## Function Documentation

## ◆ includes() [1/2]

template<typename _InputIterator1, typename _InputIterator2>

bool std::includes | ( | _InputIterator1 | __first1,
|  | _InputIterator1 | __last1,
|  | _InputIterator2 | __first2,
|  | _InputIterator2 | __last2 )
| inlinenodiscardconstexpr

Determines whether all elements of a sequence exists in a range.

Parameters

  __first1 | Start of search range.
  __last1 | End of search range.
  __first2 | Start of sequence
  __last2 | End of sequence.

Returns
  True if each element in [__first2, __last2) is contained in order within [__first1, __last1). False otherwise.

This operation expects both [__first1, __last1) and [__first2, __last2) to be sorted. Searches for the presence of each element in [__first2, __last2) within [__first1, __last1). The iterators over each range only move forward, so this is a linear algorithm. If an element in [__first2, __last2) is not found before the search iterator reaches __last2, false is returned.

Definition at line 2844 of file stl_algo.h.

## ◆ includes() [2/2]

template<typename _InputIterator1, typename _InputIterator2, typename _Compare>

bool std::includes | ( | _InputIterator1 | __first1,
|  | _InputIterator1 | __last1,
|  | _InputIterator2 | __first2,
|  | _InputIterator2 | __last2,
|  | _Compare | __comp )
| inlinenodiscardconstexpr

Determines whether all elements of a sequence exists in a range using comparison.

Parameters

  __first1 | Start of search range.
  __last1 | End of search range.
  __first2 | Start of sequence
  __last2 | End of sequence.
  __comp | Comparison function to use.

Returns
  True if each element in [__first2, __last2) is contained in order within [__first1, __last1) according to comp. False otherwise.

This operation expects both [__first1, __last1) and [__first2, __last2) to be sorted. Searches for the presence of each element in [__first2, __last2) within [__first1, __last1), using comp to decide. The iterators over each range only move forward, so this is a linear algorithm. If an element in [__first2, __last2) is not found before the search iterator reaches __last2, false is returned.

Definition at line 2890 of file stl_algo.h.

## ◆ set_difference() [1/2]

template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>

_OutputIterator std::set_difference | ( | _InputIterator1 | __first1,
|  | _InputIterator1 | __last1,
|  | _InputIterator2 | __first2,
|  | _InputIterator2 | __last2,
|  | _OutputIterator | __result )
| inlineconstexpr

Return the difference of two sorted ranges.

Parameters

  __first1 | Start of first range.
  __last1 | End of first range.
  __first2 | Start of second range.
  __last2 | End of second range.
  __result | Start of output range.

Returns
  End of the output range.

This operation iterates over both ranges, copying elements present in the first range but not the second in order to the output range. Iterators increment for each range. When the current element of the first range is less than the second, that element is copied and the iterator advances. If the current element of the second range is less, the iterator advances, but no element is copied. If an element is contained in both ranges, no elements are copied and both ranges advance. The output range may not overlap either input range.

Definition at line 5421 of file stl_algo.h.

## ◆ set_difference() [2/2]

template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare>

_OutputIterator std::set_difference | ( | _InputIterator1 | __first1,
|  | _InputIterator1 | __last1,
|  | _InputIterator2 | __first2,
|  | _InputIterator2 | __last2,
|  | _OutputIterator | __result,
|  | _Compare | __comp )
| inlineconstexpr

Return the difference of two sorted ranges using comparison function.

Parameters

  __first1 | Start of first range.
  __last1 | End of first range.
  __first2 | Start of second range.
  __last2 | End of second range.
  __result | Start of output range.
  __comp | The comparison function object.

Returns
  End of the output range.

This operation iterates over both ranges, copying elements present in the first range but not the second in order to the output range. Iterators increment for each range. When the current element of the first range is less than the second according to __comp, that element is copied and the iterator advances. If the current element of the second range is less, no element is copied and the iterator advances. If an element is contained in both ranges according to __comp, no elements are copied and both ranges advance. The output range may not overlap either input range.

Definition at line 5473 of file stl_algo.h.

## ◆ set_intersection() [1/2]

template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>

_OutputIterator std::set_intersection | ( | _InputIterator1 | __first1,
|  | _InputIterator1 | __last1,
|  | _InputIterator2 | __first2,
|  | _InputIterator2 | __last2,
|  | _OutputIterator | __result )
| inlineconstexpr

Return the intersection of two sorted ranges.

Parameters

  __first1 | Start of first range.
  __last1 | End of first range.
  __first2 | Start of second range.
  __last2 | End of second range.
  __result | Start of output range.

Returns
  End of the output range.

This operation iterates over both ranges, copying elements present in both ranges in order to the output range. Iterators increment for each range. When the current element of one range is less than the other, that iterator advances. If an element is contained in both ranges, the element from the first range is copied and both ranges advance. The output range may not overlap either input range.

Definition at line 5297 of file stl_algo.h.

## ◆ set_intersection() [2/2]

template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare>

_OutputIterator std::set_intersection | ( | _InputIterator1 | __first1,
|  | _InputIterator1 | __last1,
|  | _InputIterator2 | __first2,
|  | _InputIterator2 | __last2,
|  | _OutputIterator | __result,
|  | _Compare | __comp )
| inlineconstexpr

Return the intersection of two sorted ranges using comparison function.

Parameters

  __first1 | Start of first range.
  __last1 | End of first range.
  __first2 | Start of second range.
  __last2 | End of second range.
  __result | Start of output range.
  __comp | The comparison function object.

Returns
  End of the output range.

This operation iterates over both ranges, copying elements present in both ranges in order to the output range. Iterators increment for each range. When the current element of one range is less than the other according to __comp, that iterator advances. If an element is contained in both ranges according to __comp, the element from the first range is copied and both ranges advance. The output range may not overlap either input range.

Definition at line 5347 of file stl_algo.h.

## ◆ set_symmetric_difference() [1/2]

template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>

_OutputIterator std::set_symmetric_difference | ( | _InputIterator1 | __first1,
|  | _InputIterator1 | __last1,
|  | _InputIterator2 | __first2,
|  | _InputIterator2 | __last2,
|  | _OutputIterator | __result )
| inlineconstexpr

Return the symmetric difference of two sorted ranges.

Parameters

  __first1 | Start of first range.
  __last1 | End of first range.
  __first2 | Start of second range.
  __last2 | End of second range.
  __result | Start of output range.

Returns
  End of the output range.

This operation iterates over both ranges, copying elements present in one range but not the other in order to the output range. Iterators increment for each range. When the current element of one range is less than the other, that element is copied and the iterator advances. If an element is contained in both ranges, no elements are copied and both ranges advance. The output range may not overlap either input range.

Definition at line 5554 of file stl_algo.h.

## ◆ set_symmetric_difference() [2/2]

template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare>

_OutputIterator std::set_symmetric_difference | ( | _InputIterator1 | __first1,
|  | _InputIterator1 | __last1,
|  | _InputIterator2 | __first2,
|  | _InputIterator2 | __last2,
|  | _OutputIterator | __result,
|  | _Compare | __comp )
| inlineconstexpr

Return the symmetric difference of two sorted ranges using comparison function.

Parameters

  __first1 | Start of first range.
  __last1 | End of first range.
  __first2 | Start of second range.
  __last2 | End of second range.
  __result | Start of output range.
  __comp | The comparison function object.

Returns
  End of the output range.

This operation iterates over both ranges, copying elements present in one range but not the other in order to the output range. Iterators increment for each range. When the current element of one range is less than the other according to __comp, that element is copied and the iterator advances. If an element is contained in both ranges according to __comp, no elements are copied and both ranges advance. The output range may not overlap either input range.

Definition at line 5606 of file stl_algo.h.

## ◆ set_union() [1/2]

template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>

_OutputIterator std::set_union | ( | _InputIterator1 | __first1,
|  | _InputIterator1 | __last1,
|  | _InputIterator2 | __first2,
|  | _InputIterator2 | __last2,
|  | _OutputIterator | __result )
| inlineconstexpr

Sort the elements of a sequence using a predicate for comparison, preserving the relative order of equivalent elements.

Parameters

  __first | An iterator.
  __last | Another iterator.
  __comp | A comparison function object.

Sorts the elements in the range [__first, __last) in ascending order, such that for each iterator i in the range [__first,__last-1), __comp(*(i+1), *i)` is false.

The relative ordering of equivalent elements is preserved, so any two elements x and y in the range [__first, __last) such that __comp(x, y) is false and __comp(y, x) is false will have the same relative ordering after calling stable_sort. */ template<typename _RandomAccessIterator, typename _Compare> constexpr inline void stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) { concept requirements (_Mutable_RandomAccessIteratorConcept< _RandomAccessIterator>) (_BinaryPredicateConcept<_Compare, typename iterator_traits<_RandomAccessIterator>::value_type, typename iterator_traits<_RandomAccessIterator>::value_type>) ; ;

__stable_sort(__first, __last, __comp); }

template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare> constexpr _OutputIterator __set_union(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) { while (__first1 != __last1 && __first2 != __last2) { if (__comp(*__first1, *__first2)) { __result = *__first1; ++__first1; } else if (__comp(*__first2, *__first1)) { __result = *__first2; ++__first2; } else { __result = *__first1; ++__first1; ++__first2; } ++__result; } return std::copy(__first2, __last2, std::copy(__first1, __last1, __result)); }

/**

Return the union of two sorted ranges.

Parameters

  __first1 | Start of first range.
  __last1 | End of first range.
  __first2 | Start of second range.
  __last2 | End of second range.
  __result | Start of output range.

Returns
  End of the output range.

This operation iterates over both ranges, copying elements present in each range in order to the output range. Iterators increment for each range. When the current element of one range is less than the other, that element is copied and the iterator advanced. If an element is contained in both ranges, the element from the first range is copied and both ranges advance. The output range may not overlap either input range.

Definition at line 5176 of file stl_algo.h.

## ◆ set_union() [2/2]

template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare>

_OutputIterator std::set_union | ( | _InputIterator1 | __first1,
|  | _InputIterator1 | __last1,
|  | _InputIterator2 | __first2,
|  | _InputIterator2 | __last2,
|  | _OutputIterator | __result,
|  | _Compare | __comp )
| inlineconstexpr

Return the union of two sorted ranges using a comparison function.

Parameters

  __first1 | Start of first range.
  __last1 | End of first range.
  __first2 | Start of second range.
  __last2 | End of second range.
  __result | Start of output range.
  __comp | The comparison function object.

Returns
  End of the output range.

This operation iterates over both ranges, copying elements present in each range in order to the output range. Iterators increment for each range. When the current element of one range is less than the other according to __comp, that element is copied and the iterator advanced. If an equivalent element according to __comp is contained in both ranges, the element from the first range is copied and both ranges advance. The output range may not overlap either input range.

Definition at line 5226 of file stl_algo.h.