Apache C++ Standard Library Reference Guide

## merge()

Library: Algorithms

    Function

- Local Index
- Summary
- Synopsis
- Description
- Complexity
- Example
- See Also
- Standards Conformance

### 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

    //
    //  merge.cpp
    //

    #include <algorithm>    // advance, copy, inplace_merge, merge
    #include <functional>   // less
    #include <iostream>     // cout
    #include <iterator>     // back_inserter, ostream_iterator
    #include <vector>       // vector

    int main ()
    {
        typedef std::vector<int, std::allocator<int> > Vector;

        const Vector::value_type d1[] = { 1, 2, 3, 4};
        const Vector::value_type d2[] = { 11, 13, 15, 17,
                                          12, 14, 16, 18};

        // Set up two vectors.
        Vector v1 (d1 + 0, d1 + sizeof d1 / sizeof *d1);
        Vector v2 (d1 + 0, d1 + sizeof d1 / sizeof *d1);

        // Set up four destination vectors.
        Vector v3 (d2 + 0, d2 + sizeof d2 / sizeof *d2);
        Vector v4 (v3);
        Vector v5 (v3);
        Vector v6 (v3);

        // Set up one empty vector.
        Vector v7;

        // Merge v1 with v2.
        std::merge (v1.begin (), v1.end (),
                    v2.begin (), v2.end (), v3.begin ());

        // Now use comparator.
        std::merge (v1.begin (), v1.end (),
                    v2.begin (), v2.end (), v4.begin (),
                    std::less<int>());

        // In place merge v5.
        Vector::iterator mid = v5.begin ();
        std::advance (mid, 4);   // equivalent to mid += 4
                                 // but more generic
        std::inplace_merge (v5.begin (), mid, v5.end ());

        // Now use a comparator on v6.
        mid = v6.begin ();
        std::advance (mid, 4);   // equivalent to mid += 4
                                 // but more generic
        std::inplace_merge (v6.begin (), mid, v6.end (),
                            std::less<int>());

        // Merge v1 and v2 to empty vector using insert iterator.
        std::merge (v1.begin (), v1.end (),
                    v2.begin (), v2.end (),
                    std::back_inserter (v7));

        // Copy all to cout.
        std::ostream_iterator<int, char, std::char_traits<char> >
            out (std::cout," ");

        std::copy (v1.begin (), v1.end (), out);
        std::cout << std::endl;
        std::copy (v2.begin(), v2.end (), out);
        std::cout << std::endl;
        std::copy (v3.begin (), v3.end (), out);
        std::cout << std::endl;
        std::copy (v4.begin (), v4.end (), out);
        std::cout << std::endl;
        std::copy (v5.begin (), v5.end (), out);
        std::cout << std::endl;
        std::copy (v6.begin (),v6.end (), out);
        std::cout << std::endl;
        std::copy (v7.begin (), v7.end (), out);
        std::cout << std::endl;

        // Merge v1 and v2 to cout.
        std::merge (v1.begin (), v1.end (),
                    v2.begin (), v2.end (), out);

        return 0;
    }

    Program Output:

    1 2 3 4
    1 2 3 4
    1 1 2 2 3 3 4 4
    1 1 2 2 3 3 4 4
    11 12 13 14 15 16 17 18
    11 12 13 14 15 16 17 18
    1 1 2 2 3 3 4 4
    1 1 2 2 3 3 4 4

### See Also

Containers, `inplace_merge()`

### Standards Conformance

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