This site claims that set_union
is equivalent to the following code:
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_union ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result )
{
while (true)
{
if (*first1<*first2) *result++ = *first1++;
else if (*first2<*first1) *result++ = *first2++;
else { *result++ = *first1++; first2++; }
if (first1==last1) return copy(first2,last2,result);
if (first2==last2) return copy(first1,last1,result);
}
}
But that seems strange: Won't that crash (or result in other undefined behavior) if one of the ranges is empty? Shouldn't the two if
clauses be at the beginning of the while
loop, instead of the end?