std::ranges::set_union()
and std::ranges::merge()
are both used to combine two sorted ranges into a single sorted range, but they serve different purposes and have unique benefits.
std::ranges::merge()
The std::ranges::merge()
algorithm combines two sorted ranges into one, including all elements from both ranges. If there are duplicate elements, they will appear in the output:
#include <algorithm>
#include <iostream>
#include <ranges>
#include <vector>
int main() {
std::vector<int> vec1{1, 3, 5};
std::vector<int> vec2{2, 3, 6};
std::vector<int> result;
result.resize(vec1.size() + vec2.size());
std::ranges::merge(vec1, vec2, result.begin());
for (int n : result) {
std::cout << n << ", ";
}
}
1, 2, 3, 3, 5, 6,
std::ranges::set_union()
The std::ranges::set_union()
algorithm also combines two sorted ranges into one but removes duplicates. This is useful when you need a union of two sets without repeated elements:
#include <algorithm>
#include <iostream>
#include <ranges>
#include <vector>
int main() {
std::vector<int> vec1{1, 3, 5};
std::vector<int> vec2{2, 3, 6};
std::vector<int> result;
result.resize(vec1.size() + vec2.size());
auto it = std::ranges::set_union(
vec1, vec2, result.begin());
result.erase(it.out, result.end());
for (int n : result) {
std::cout << n << ", ";
}
}
1, 2, 3, 5, 6,
std::ranges::set_union()
Use std::ranges::merge()
:
Use std::ranges::set_union()
:
Both algorithms are efficient, but their performance can vary based on the context:
std::ranges::merge()
might be faster when handling large data sets with many duplicates since it does not need to check for uniqueness.std::ranges::set_union()
can save time and memory by avoiding duplicate elements, making it more efficient in scenarios where uniqueness is required.In summary, std::ranges::set_union()
is beneficial for combining ranges while ensuring uniqueness, making it ideal for set operations.
In contrast, std::ranges::merge()
is better suited for scenarios where all elements, including duplicates, need to be preserved.
Understanding the specific requirements of your application will help you choose the appropriate algorithm.
Answers to questions are automatically generated and may not have been reviewed.
An introduction to 8 more useful algorithms from the standard library, and how we can use them alongside views, projections, and other techniques