You can use the two-iterator std::set::insert
template for this:
template
std::set getUnion(const std::set& a, const std::set& b)
{
std::set result = a;
result.insert(b.begin(), b.end());
return result;
}
Note: Following some of the comments suggesting I take one of the parameters by value because I need a copy anyway, I chose this implementation to avoid disallowing RVO, which is not allowed when returning parameter taken by value. To better deal with rvalue arguments, overloads of this function taking rvalue reverences and leveraging move semantics could be provided.