It is generally OK. However it is for that generic case of how
operator<(type1,type2) exists and then that destination range
accepts type1. When I care about performance then I do not make it
generic silver bullet as such don't exist.
Instead I take into account every thing about given input sequences,
given type, output sequence and constraints to resource usage.
Isn't it usually same type? Can't that type usually be cheaply hashed?
Are the sequence lengths usually similar or different? Is there plenty
of memory? Can there be several equal values in input sequences? Is
the output iterator really output iterator or just pointer of buffer
that I can sort afterwards? Is it even needed that result is sorted?
Making two equal capacity hash multisets and intersecting those and
then sorting the result is bit cheaper in my tests than your algorithm
but uses more memory.
Paavo Helde's suggestion is also great when hashing is unavailable
or memory is precious, especially when size difference between
two sequences is major but it takes some trick of marking
already matched elements on case the sequences can contain multiple
equal elements.