cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Algorithms : merge
 
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Algorithms
algorithm:
· adjacent_find
· binary_search
· copy
· copy_backward
· count
· count_if
· equal
· equal_range
· fill
· fill_n
· find
· find_end
· find_first_of
· find_if
· for_each
· generate
· generate_n
· includes
· inplace_merge
· iter_swap
· lexicographical_compare
· lower_bound
· make_heap
· max
· max_element
· merge
· min
· min_element
· mismatch
· next_permutation
· nth_element
· partial_sort
· partial_sort_copy
· partition
· pop_heap
· prev_permutation
· push_heap
· random_shuffle
· remove
· remove_copy
· remove_copy_if
· remove_if
· replace
· replace_copy
· replace_copy_if
· replace_if
· reverse
· reverse_copy
· rotate
· rotate_copy
· search
· search_n
· set_difference
· set_intersection
· set_symmetric_difference
· set_union
· sort
· sort_heap
· stable_partition
· stable_sort
· swap
· swap_ranges
· transform
· unique
· unique_copy
· upper_bound

-

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

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

Merge sorted ranges

Combines the elements in the sorted ranges [first1,last1) and [first2,last2), into a new range beginning at result with its elements sorted. The comparison for sorting uses either operator< for the first version, or comp for the second.

For the function to yield the expected result, the elements in the both ranges shall already be ordered according to the same strict weak ordering criterion (operator< or comp). The resulting range is also sorted according to it.

The behavior of this function template is equivalent to:

template <class InputIterator1, class InputIterator2, class OutputIterator>
  OutputIterator merge ( InputIterator1 first1, InputIterator1 last1,
                         InputIterator2 first2, InputIterator2 last2,
                         OutputIterator result )
{
  while (true) {
    *result++ = (*first2<*first1)? *first2++ : *first1++;
    if (first1==last1) return copy(first2,last2,result);
    if (first2==last2) return copy(first1,last1,result);
  }
}

Parameters

first1, last1
Input iterators to the initial and final positions of the first sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.
first2, last2
Input iterators to the initial and final positions of the second sequence. The range used is [first2,last2).
result
Output iterator to the initial position of the range where the resulting combined range is stored. Its length is equal to the addition of the lengths of both previous ranges.
comp
Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.

Return value

An iterator pointing to the past-the-end element in the resulting sequence.

Example

// merge algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main () {
  int first[] = {5,10,15,20,25};
  int second[] = {50,40,30,20,10};
  vector<int> v(10);
  vector<int>::iterator it;

  sort (first,first+5);
  sort (second,second+5);
  merge (first,first+5,second,second+5,v.begin());

  cout << "The resulting vector contains:";
  for (it=v.begin(); it!=v.end(); ++it)
    cout << " " << *it;

  cout << endl;
  
  return 0;
}

Output:

The resulting vector contains: 5 10 10 15 20 20 25 30 40 50

Complexity

At most, performs count1+count2-1 assignments and comparisons or applications of comp (where countX is the distance between firstX and lastX).

See also

inplace_merge Merge consecutive sorted ranges (function template)
set_union Union of two sorted ranges (function template)
copy Copy range of elements (function template)
© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us