Set Union with Unsorted Inputs

What happens if the input sets for set_union() are not sorted?

If the input sets for std::ranges::set_union() are not sorted, the behavior of the algorithm is undefined. This means you cannot rely on it to produce correct or consistent results.

The std::ranges::set_union() algorithm assumes that both input ranges are sorted in ascending order by default, or according to a provided comparator if specified.

Here's what could happen with unsorted inputs:

  1. Incorrect Results: The union might not contain all unique elements from both sets.
  2. Duplicates: The result may include duplicates even if the inputs are supposed to represent sets (which by definition should not have duplicates).
  3. Unpredictable Behavior: The algorithm might not work correctly, leading to unexpected outputs.

Consider this example with unsorted inputs:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  std::vector<int> A{3, 1, 2};
  std::vector<int> B{4, 5, 3};
  std::vector<int> Results;
  Results.resize(A.size() + B.size());

  std::ranges::set_union(A, B, Results.begin());
  for (auto x : Results) {
    std::cout << x << ", ";
  }
}
3, 1, 2, 4, 5, 3,

In this output, you can see duplicates due to the unsorted inputs leading to undefined behavior.

Sorting Before Union

To ensure correct results, always sort the input ranges before calling std::ranges::set_union(). Here's how to do it:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  std::vector<int> A{3, 1, 2};
  std::vector<int> B{4, 5, 3};
  std::vector<int> Results;
  Results.resize(A.size() + B.size());

  std::sort(A.begin(), A.end());
  std::sort(B.begin(), B.end());

  auto[last1, last2, result_last] =
    std::ranges::set_union(A, B, Results.begin());

  Results.erase(result_last, Results.end());

  for (auto x : Results) {
    std::cout << x << ", ";
  }
}
1, 2, 3, 4, 5,

Here, std::sort() is used to sort A and B before performing the union. The result is the correct union of the two sets.

Always ensure your inputs are sorted to guarantee correct behavior when using std::ranges::set_union().

Set Algorithms

An introduction to set algorithms, and how to implement them using the C++ standard library

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Using Custom Comparators
How can I use includes() with a custom comparator?
Set Intersection with Different Containers
Can set_intersection() be used with different types of containers?
Difference vs Symmetric Difference
What is the difference between set_difference() and set_symmetric_difference()?
Handling Duplicates in Set Union
How do I handle duplicates in set_union()?
Parallelizing Set Algorithms
Can set algorithms be parallelized for performance?
Ensuring Sorted Inputs for Set Algorithms
How do I ensure my sets are sorted correctly before using set algorithms?
Examples of Symmetric Difference
What are some practical examples of using set_symmetric_difference()?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant