merge

将所有从两个排序的源范围的元素到单个,排序的目标范围,该排序的标准可以由二进制谓词指定。

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 BinaryPredicate>
   OutputIterator merge(
      InputIterator1 _First1, 
      InputIterator1 _Last1,
      InputIterator2 _First2, 
      InputIterator2 _Last2, 
      OutputIterator _Result
      BinaryPredicate _Comp
   );

参数

  • _First1
    解决输入的迭代器第一个元素的位置在第一个排序源范围合并和排序到一个范围。

  • _Last1
    解决输入的迭代器通过最后一个元素的位置一在第一个排序源范围合并和排序到一个范围。

  • _First2
    解决输入的迭代器第一个元素的位置在第二两个连续的排序的源范围合并和排序到一个范围。

  • _Last2
    解决输入的迭代器通过最后一个元素的位置一在第二两个连续的排序的源范围合并和排序到一个范围。

  • _Result
    解决输出的迭代器第一个元素的位置两个源范围将合并到单个排序范围的目标范围。

  • _Comp
    定义含义的用户定义的谓词函数对象哪个元素比另一个大。 二进制谓词采用两个参数,并应返回 true ,当第一个元素比否则第二个元素和 false 小于时。

返回值

解决输出的迭代器通过最后一个元素的位置一在已排序的目标范围。

备注

引用的排序的源范围必须是有效的;所有指针必须dereferenceable,在每个序列中最后位置必须是可访问的从开始按增量。

目标范围不应重叠源范围,应足以包含目标范围。

必须每个位置排序的源范围,则为 merge 算法的应用程序的前提条件与排序的相同与与将算法使用排序合并的大小。

因为组件相对顺序在每个范围内的在目标范围,保留操作是否稳定。 算法不修改源范围 merge

输入迭代器的值类型需要小于可将排序,因此,命名两个组件,可以确定为它们是等效的(来讲都比其他不小于)或一个比其他小于。 这将导致排序在非等价的元素之间。 当在两个源范围中的等效的元素,第一个范围的元素位于从第二个源范围的元素在目标范围。

算法的复杂是线性与至多(_Last1 – _First1) (_Last2 – _First2) – 1次比较。

列表 选件类提供一个成员函数 合并 组合元素的两个列表。

merge 有两个相关的窗体:

checked_merge

unchecked_merge

有关这些功能如何的信息的行为,请参见 经过检查的迭代器

示例

// alg_merge.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>   // For greater<int>( )
#include <iostream>

// Return whether modulus of elem1 is less than modulus of elem2
bool mod_lesser ( int elem1, int elem2 ) {
   if (elem1 < 0) 
      elem1 = - elem1;
   if (elem2 < 0) 
      elem2 = - elem2;
   return elem1 < elem2;
}

int main() {
   using namespace std;
   vector <int> v1a, v1b, v1 ( 12 );
   vector <int>::iterator Iter1a,  Iter1b, Iter1;

   // Constructing vector v1a and v1b with default less than ordering
   int i;
   for ( i = 0 ; i <= 5 ; i++ )
      v1a.push_back(  i );

   int ii;
   for ( ii =-5 ; ii <= 0 ; ii++ )
      v1b.push_back(  ii  );

   cout << "Original vector v1a with range sorted by the\n "
        << "binary predicate less than is  v1a = ( " ;
   for ( Iter1a = v1a.begin( ) ; Iter1a != v1a.end( ) ; Iter1a++ )
      cout << *Iter1a << " ";
   cout << ")." << endl;

   cout << "Original vector v1b with range sorted by the\n "
        << "binary predicate less than is  v1b = ( " ;
   for ( Iter1b = v1b.begin ( ) ; Iter1b != v1b.end ( ) ; Iter1b++ )
      cout << *Iter1b << " ";
   cout << ")." << endl;
   
   // Constructing vector v2 with ranges sorted by greater
   vector <int> v2a ( v1a ) , v2b ( v1b ) ,  v2 ( v1 );
   vector <int>::iterator Iter2a,  Iter2b, Iter2;
   sort ( v2a.begin ( ) , v2a.end ( ) , greater<int> ( ) );
   sort ( v2b.begin ( ) , v2b.end ( ) , greater<int> ( ) );

   cout << "Original vector v2a with range sorted by the\n "
        <<  "binary predicate greater is   v2a =  ( " ;
   for ( Iter2a = v2a.begin ( ) ; Iter2a != v2a.end ( ) ; Iter2a++ )
      cout << *Iter2a << " ";
   cout << ")." << endl;

   cout << "Original vector v2b with range sorted by the\n "
        <<  "binary predicate greater is   v2b =  ( " ;
   for ( Iter2b = v2b.begin ( ) ; Iter2b != v2b.end ( ) ; Iter2b++ )
      cout << *Iter2b << " ";
   cout << ")." << endl;

   // Constructing vector v3 with ranges sorted by mod_lesser
   vector <int> v3a ( v1a ), v3b ( v1b ) ,  v3 ( v1 );
   vector <int>::iterator Iter3a,  Iter3b, Iter3;
   sort ( v3a.begin ( ) , v3a.end ( ) , mod_lesser );
   sort ( v3b.begin ( ) , v3b.end ( ) , mod_lesser );

   cout << "Original vector v3a with range sorted by the\n "
        << "binary predicate mod_lesser is   v3a =  ( " ;
   for ( Iter3a = v3a.begin ( ) ; Iter3a != v3a.end ( ) ; Iter3a++ )
      cout << *Iter3a << " ";
   cout << ")." << endl;

   cout << "Original vector v3b with range sorted by the\n "
        << "binary predicate mod_lesser is   v3b =  ( " ;
   for ( Iter3b = v3b.begin ( ) ; Iter3b != v3b.end ( ) ; Iter3b++ )
      cout << *Iter3b << " ";
   cout << ")." << endl;

   // To merge inplace in ascending order with default binary 
   // predicate less <int> ( )
   merge ( v1a.begin ( ) , v1a.end ( ) , v1b.begin ( ) , v1b.end ( ) , v1.begin ( ) );
   cout << "Merged inplace with default order,\n vector v1mod =  ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   // To merge inplace in descending order, specify binary 
   // predicate greater<int>( )
   merge ( v2a.begin ( ) , v2a.end ( ) , v2b.begin ( ) , v2b.end ( ) ,
       v2.begin ( ) ,  greater <int> ( ) );
   cout << "Merged inplace with binary predicate greater specified,\n "
        << "vector v2mod  = ( " ;
   for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
      cout << *Iter2 << " ";
   cout << ")." << endl;

   // Applying A user-defined (UD) binary predicate mod_lesser
   merge ( v3a.begin ( ) , v3a.end ( ) , v3b.begin ( ) , v3b.end ( ) ,
       v3.begin ( ) ,  mod_lesser );
   cout << "Merged inplace with binary predicate mod_lesser specified,\n "
        << "vector v3mod  = ( " ; ;
   for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
      cout << *Iter3 << " ";
   cout << ")." << endl;
}

Output

Original vector v1a with range sorted by the
 binary predicate less than is  v1a = ( 0 1 2 3 4 5 ).
Original vector v1b with range sorted by the
 binary predicate less than is  v1b = ( -5 -4 -3 -2 -1 0 ).
Original vector v2a with range sorted by the
 binary predicate greater is   v2a =  ( 5 4 3 2 1 0 ).
Original vector v2b with range sorted by the
 binary predicate greater is   v2b =  ( 0 -1 -2 -3 -4 -5 ).
Original vector v3a with range sorted by the
 binary predicate mod_lesser is   v3a =  ( 0 1 2 3 4 5 ).
Original vector v3b with range sorted by the
 binary predicate mod_lesser is   v3b =  ( 0 -1 -2 -3 -4 -5 ).
Merged inplace with default order,
 vector v1mod =  ( -5 -4 -3 -2 -1 0 0 1 2 3 4 5 ).
Merged inplace with binary predicate greater specified,
 vector v2mod  = ( 5 4 3 2 1 0 0 -1 -2 -3 -4 -5 ).
Merged inplace with binary predicate mod_lesser specified,
 vector v3mod  = ( 0 0 1 -1 2 -2 3 -3 4 -4 5 -5 ).

要求

标头: <algorithm>

命名空间: std

请参见

参考

merge (STL Samples)

Predicate Version of merge

标准模板库