Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library Reference Guide

merge()

Library:  Algorithms


Function

Local Index

No Entries

Summary

Algorithm that merges two sorted sequences into a third disjoint sorted sequence

Synopsis

#include <algorithm>

namespace std {
  template <class InputIterator1, class InputIterator2,
            class OutputIterator>
  OutputIterator
  merge(InputIterator1 start1, InputIterator1 finish1,
        InputIterator2 start2, InputIterator2 finish2,
        OutputIterator result);

  template <class InputIterator1, class InputIterator2,
            class OutputIterator, class Compare>
  OutputIterator
  merge(InputIterator1 start1, InputIterator1 finish1,
        InputIterator2 start2, InputIterator2 finish2,
        OutputIterator result, Compare comp);
}

Description

The merge() algorithm merges two sorted sequences, specified by [start1, finish1) and [start2, finish2), into the sequence specified by [result, result + (finish1 - start1) + (finish2 - start2)). The first version of the merge() algorithm uses operator<() to compare elements in the two sequences. The second version uses the function object comp. If a comparison function is provided, merge() assumes that both sequences were sorted using the same ordering as the one given by the function object.

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 + (finish1 - start1) + (finish2 -start2)). The result of merge() is undefined if the resulting range overlaps with either of the original ranges.

merge() assumes that all iterators in the range [result, result + (finish1 - start1) + (finish2 - start2)] are dereferenceable, unless result has been adapted by an insert iterator.

Complexity

At most (finish - start1) + (finish2 - start2) - 1 comparisons are performed.

Example

See Also

Containers, inplace_merge()

Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.3.4



Previous fileTop of DocumentContentsIndex pageNext file