C++ 算法 中的 merge函数

首页 / C++入门教程 / C++ 算法 中的 merge函数

C++算法 merge()函数用于将两个排序参数[first1,last1)和[first2,last2)合并为一个从结果开始的排序参数。

merge - 语法

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

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

merge - 参数

first1 :一个输入迭代器,它指向要合并的第一个排序的源序列中的第一个元素。

last:一个输入迭代器,它指向要合并的第一个排序源序列中的最后一个元素。

first2 :一个输入迭代器,它指向要合并的第二个已排序源序列中的第一个元素。

last2 :一个输入迭代器,它指向要合并的第二个已排序源序列中的最后一个元素。

comp :用户定义的二进制谓词函数,该函数接受两个参数,如果两个参数顺序正确,则返回true,否则返回false。

val :比较参数内元素的上限值。

result:一个输出迭代器,该迭代器指向目标参数中的第一个元素,其中两个源参数将合并为一个排序参数。

merge - 返回值

它返回一个指向结果序列中最后一个元素的迭代器。

merge - 例子1

让我们看一个简单的示例来演示merge()的用法:

#include <iostream>    
#include <algorithm>   
#include <vector>       
using namespace std;
 
void printVector(vector<int>& v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
        cout << ' ' << *it;
    cout << '\n';
}
 
int main () {
    vector<int> v1 = {5,1,4,2,6}, v2 = {50,40,30,20,10}, v3(10);
 
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
 
    cout << "Vector v1 : ";
    printVector(v1);
    cout << "Vector v2 : ";
    printVector(v2);
    cout << "Vector v3 : ";
    printVector(v3);
    
    return 0;
}

输出:

Vector v1 :  1 2 4 5 6
Vector v2 :  10 20 30 40 50
Vector v3 :  1 2 4 5 6 10 20 30 40 50

merge - 例子2

我们来看另一个简单的示例e,该示例使用operator <来实现merge()函数

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std; 
  
int main() 
{ 
   //initializing 1st container 
    vector<int> arr1 = { 1, 4, 6, 3, 2 }; 
  
   //initializing 2nd container 
    vector<int> arr2 = { 60, 20, 50, 70, 10 }; 
  
   //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 2 3 4 6 10 20 50 60 70

merge - 例子3

让我们看另一个简单的示例,使用比较函数演示merge()

#include <iostream>
#include <vector>
#include <algorithm>

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 = { 60, 20, 50, 70, 10 }; 
  
   //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 : 70 60 50 20 10 6 4 3 2 1

merge - 例子4

我们来看另一个简单的例子

#include <iostream>    
#include <algorithm>   
#include <vector>       
using namespace std;
 
int main() 
{ 
   //initializing 1st container 
   //containing denominations 
    vector<int> stack1 = { 50, 20, 10, 100, 200 }; 
  
   //initializing 2nd container 
   //containing demonitions 
    vector<int> stack2 = { 500, 2000, 5000, 1000, 10000 }; 
  
   //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 200 
The original 2nd stack: 500 2000 5000 1000 10000 
The resultant stack of notes is: 10 20 50 100 200 500 1000 2000 5000 10000 

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

邱岳的产品实战 -〔邱岳〕

Kafka核心技术与实战 -〔胡夕〕

Flutter核心技术与实战 -〔陈航〕

性能工程高手课 -〔庄振运〕

郭东白的架构课 -〔郭东白〕

说透元宇宙 -〔方军〕

结构学习力 -〔李忠秋〕

Rust 语言从入门到实战 -〔唐刚〕

工程师个人发展指南 -〔李云〕

好记忆不如烂笔头。留下您的足迹吧 :)