Combines the elements in the sorted ranges [first1,last1)
and [first2,last2)
, into a new range beginning at result with all its elements sorted.
The elements are compared using operator<
for the first version, and comp for the second.
The elements in both ranges shall already be ordered according to this same criterion (operator<
or comp). The resulting range is also sorted according to this.
template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator merge(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result) { while (true) { if (first1==last1) return std::copy(first2,last2,result); if (first2==last2) return std::copy(first1,last1,result); *result++ = (*first2<*first1)? *first2++ : *first1++; } }