當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ merge()用法及代碼示例


C++在其STL庫中提供了merge(),它對於將排序兩個容器合並為一個容器非常有用。它在標題“算法”中定義。它以兩種方式實現。

語法1:使用運算符“ <”

Template:
template 
  outiter merge (initer1 beg1, initer1 end1,
                        initer2 beg2, initer2 end2,
                        outiter res)

參數:
beg1:  Input iterator to initial position of first sequence.
end1:  Input iterator to final position of first sequence.

beg2:  Input iterator to initial position of second sequence.
end2:  Input iterator to final position of second sequence.

res: Output Iterator to initial position of resultant container.
返回值:
Iterator to last element of the resulting container.

// C++ code to demonstrate the working of 
// merge() implementation 1 
  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initializing 1st container 
    vector<int> arr1 = { 1, 4, 6, 3, 2 }; 
  
    // initializing 2nd container 
    vector<int> arr2 = { 6, 2, 5, 7, 1 }; 
  
    // declaring resultant container 
    vector<int> arr3(10); 
  
    // sorting initial containers 
    sort(arr1.begin(), arr1.end()); 
    sort(arr2.begin(), arr2.end()); 
  
    // using merge() to merge the initial containers 
    merge(arr1.begin(), arr1.end(), arr2.begin(), arr2.end(), arr3.begin()); 
  
    // printing the resultant merged container 
    cout << "The container after merging initial containers is:"; 
  
    for (int i = 0; i < arr3.size(); i++) 
        cout << arr3[i] << " "; 
    return 0; 
}

輸出:


The container after merging initial containers is:1 1 2 2 3 4 5 6 6 7 

語法2:使用比較器函數

Template:
template 
  outiter merge (initer1 beg1, initer1 end1,
                        initer2 beg2, initer2 end2,
                        outiter res, Compare comp)

參數:
beg1:  Input iterator to initial position of first sequence.
end1:  Input iterator to final position of first sequence.

beg2:  Input iterator to initial position of second sequence.
end2:  Input iterator to final position of second sequence.

res: Output Iterator to initial position of resultant container.
comp: The comparator function that returns a boolean
true/false of the each elements compared. This function 
accepts two arguments. This can be function pointer or 
function object and cannot change values.
返回值:
Iterator to last element of the resulting container.

// C++ code to demonstrate the working of 
// merge() implementation 2 
  
#include <bits/stdc++.h> 
using namespace std; 
  
// comparator function to reverse merge sort 
struct greaters { 
    bool operator()(const long& a, const long& b) const
    { 
        return a > b; 
    } 
}; 
  
int main() 
{ 
    // initializing 1st container 
    vector<int> arr1 = { 1, 4, 6, 3, 2 }; 
  
    // initializing 2nd container 
    vector<int> arr2 = { 6, 2, 5, 7, 1 }; 
  
    // declaring resultant container 
    vector<int> arr3(10); 
  
    // sorting initial containers 
    // in descending order 
    sort(arr1.rbegin(), arr1.rend()); 
    sort(arr2.rbegin(), arr2.rend()); 
  
    // using merge() to merge the initial containers 
    // returns descended merged container 
    merge(arr1.begin(), arr1.end(), arr2.begin(), arr2.end(), arr3.begin(), greaters()); 
  
    // printing the resultant merged container 
    cout << "The container after reverse merging initial containers is:"; 
  
    for (int i = 0; i < arr3.size(); i++) 
        cout << arr3[i] << " "; 
    return 0; 
}

輸出:

The container after reverse merging initial containers is:7 6 6 5 4 3 2 2 1 1 

可能的應用:合並函數可用於按排序順序提供兩個堆棧的單個堆棧。這些可以是一堆書或筆記。讓我們討論一個簡單的示例,該示例根據其值將升序的兩疊鈔票合並為一個。

// C++ code to demonstrate the application of 
// merge() stacking notes 
  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initializing 1st container 
    // containing denominations 
    vector<int> stack1 = { 50, 20, 10, 100, 2000 }; 
  
    // initializing 2nd container 
    // containing demonitions 
    vector<int> stack2 = { 500, 2000, 10, 100, 50 }; 
  
    // declaring resultant stack 
    vector<int> stack3(10); 
  
    cout << "The original 1st stack:"; 
    for (int i = 0; i < 5; i++) 
        cout << stack1[i] << " "; 
  
    cout << endl; 
  
    cout << "The original 2nd stack:"; 
    for (int i = 0; i < 5; i++) 
        cout << stack2[i] << " "; 
  
    cout << endl; 
  
    // sorting initial stacks of notes 
    // in descending order 
    sort(stack1.begin(), stack1.end()); 
    sort(stack2.begin(), stack2.end()); 
  
    // using merge() to merge the initial stacks 
    // of notes 
    merge(stack1.begin(), stack1.end(), stack2.begin(), stack2.end(), stack3.begin()); 
  
    // printing the resultant stack 
    cout << "The resultant stack of notes is:"; 
  
    for (int i = 0; i < stack3.size(); i++) 
        cout << stack3[i] << " "; 
    return 0; 
}

輸出:

The original 1st stack:50 20 10 100 2000 
The original 2nd stack:500 2000 10 100 50 
The resultant stack of notes is:10 10 20 50 50 100 100 500 2000 2000 



相關用法


注:本文由純淨天空篩選整理自 merge() in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。