Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

merge


Algorithm

Summary

Merges two sorted sequences into a third sequence.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

None

Synopsis

#include <algorithm>
template <class InputIterator1, class InputIterator2,
          class OutputIterator>
  OutputIterator
  merge(InputIterator first1, InputIterator1 last1,
        InputIterator2 first2, InputIterator last2,
        OutputIterator result);

template <class InputIterator1, class InputIterator2,
          class OutputIterator, class Compare>
  OutputIterator
  merge(InputIterator1 first1, InputIterator1 last1,
        InputIterator2 first2, InputIterator last2,
        OutputIterator result, Compare comp);

Description

The merge algorithm merges two sorted sequences, specified by [first1, last1) and [first2, last2), into the sequence specified by [result, result + (last1 - first1) + (last2 - first2)). The first version of the merge algorithm uses the less than operator (<) to compare elements in the two sequences. The second version uses the comparison function included in the function call. If a comparison function is included, merge assumes that both sequences were sorted using that comparison function.

The merge is stable. This means that if the two original sequences contain equivalent elements, the elements from the first sequence always precede the matching elements from the second in the resulting sequence. The size of the result of a merge is equal to the sum of the sizes of the two argument sequences. merge returns an iterator that points to the end of the resulting sequence (in other words, result + (last1 - first1) + (last2 -first2)). The result of merge is undefined if the resulting range overlaps with either of the original ranges.

merge assumes that there are at least (last1 - first1) + (last2 - first2) elements following result, unless result has been adapted by an insert iterator.

Complexity

At most (last - first1) + (last2 - first2) - 1 comparisons are performed.

Example

Program Output

Warnings

If your compiler does not support default template parameters, then you always need to supply the Allocator template argument. For instance, you have to write:

vector<int,allocator<int> >

instead of:

vector<int>

If your compiler does not support namespaces, then you do not need the using declaration for std.

See Also

Containers, inplace_merge



Previous fileTop of documentContentsIndexNext file
©Copyright 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.
OEM Release, June 1998