共用方式為


合併

合併所有從兩個排序的來源範圍的項目到單一,排序的目的範圍,排序準則可能由二進位述詞指定。

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
    輸入定址的 Iterator 第一個項目的位置在第一個排序來源範圍合併和排序成單一範圍。

  • _Last1
    輸入定址的 Iterator 將最後一個項目的位置會在第一個排序來源範圍合併和排序成單一範圍。

  • _First2
    輸入定址的 Iterator 第一個項目的位置在第二個兩個連續排序的來源範圍合併和排序成單一範圍。

  • _Last2
    輸入定址的 Iterator 將最後一個項目的位置會在第二個兩個連續排序的來源範圍合併和排序成單一範圍。

  • _Result
    輸出定址的 Iterator 的第一個項目位置在兩個來源範圍要合併成單一排序範圍的目的範圍。

  • _Comp
    使用者定義的述詞函式物件,定義在此意義上某個項目大於另一個。 如果第一個項目則為時,小於第二個項目和 false 二進位述詞會採用兩個引數,則應該傳回 true

傳回值

輸出定址的 Iterator 將最後一個項目的位置一個排序的目的範圍。

備註

參考的排序的來源範圍必須是有效的;所有指標必須 dereferenceable,並且每個序列中的最後位置必須取用開頭 (由增量。

目的範圍不得重疊的區域,而且應該足以包含目的範圍。

其中每個引數都必須具有排序的來源範圍,則為指定之演算法的應用程式的前提是符合定序與將演算法使用排序合併的範圍。

因為項目的相對順序。每個範圍內的目的範圍,儲存作業不穩定。 演算法不會修改來源範圍 merge

輸入 Iterator 的實值型別必須是小於比較要排序,,因此將兩個項目,可以判斷為它們相等 (所以都不小於其他) 或一個小於其他。 這會導致在非對等元件之間的排序 當有兩個來源範圍時的對等項目,在第一個範圍的元素中從第二個來源範圍的項目之前在目的範圍。

演算法的複雜是線性與至多 (_Last1 – _First1) (_Last2 – _First2) – 1 次比較。

清單 類別提供 10% 成員函式 合併 結合兩份清單的項目。

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 範例)

merge 的述詞版本

標準樣板程式庫