代码合集(1)——容器算法之合并 merge

本文介绍了C++标准库中的排序算法及集合操作函数,包括merge、inplace_merge等用于合并排序的功能,以及includes、set_union等用于集合运算的操作。通过示例展示了如何使用这些函数对序列进行操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、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 Compare>
OutputIterator merge ( InputIterator1 first1, InputIterator1 last1,
                         InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp );
示例:

// 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;
}


2、inplace_merge:将两个序列合并成一个新的序列,并对新的序列进行归并排序(这两个序列必须要进过排序)
原型:
template <class BidirectionalIterator>
void inplace_merge ( BidirectionalIterator first, BidirectionalIterator middle,
                       BidirectionalIterator last );

template <class BidirectionalIterator, class Compare>
 void inplace_merge ( BidirectionalIterator first, BidirectionalIterator middle,
                       BidirectionalIterator last, Compare comp );
示例:

// inplace_merge 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);

  copy (first,first+5,v.begin());
  copy (second,second+5,v.begin()+5);

  inplace_merge (v.begin(),v.begin()+5,v.end());

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

  cout << endl;
  
  return 0;
}


 

3、includes:测试是一个序列是否在另一个序列中
原型:
template <class InputIterator1, class InputIterator2> 
bool includes ( InputIterator1 first1, InputIterator1 last1, 
                InputIterator2 first2, InputIterator2 last2 ); 
template <class InputIterator1, class InputIterator2, class Compare> 
bool includes ( InputIterator1 first1, InputIterator1 last1, 
                InputIterator2 first2, InputIterator2 last2, Compare comp );
示例:

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

bool myfunction (int i, int j) { return i<j; }

int main () {
  int container[] = {5,10,15,20,25,30,35,40,45,50};
  int continent[] = {40,30,20,10};

  sort (container,container+10);
  sort (continent,continent+4);

  // using default comparison:
  if ( includes(container,container+10,continent,continent+4) )
    cout << "container includes continent!" << endl;

  // using myfunction as comp:
  if ( includes(container,container+10,continent,continent+4, myfunction) )
    cout << "container includes continent!" << endl;

  return 0;
}


 

4、set_union:和merge类似,不过新序列中没有重复的元素
原型:
template <class InputIterator1, class InputIterator2, class OutputIterator>  
OutputIterator set_union ( InputIterator1 first1, InputIterator1 last1,          
                           InputIterator2 first2, InputIterator2 last2,OutputIterator result );
template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>  
OutputIterator set_union ( InputIterator1 first1, InputIterator1 last1, 
                           InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp );
示例:

// set_union 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);                           // 0  0  0  0  0  0  0  0  0  0
  vector<int>::iterator it;

  sort (first,first+5);     //  5 10 15 20 25
  sort (second,second+5);   // 10 20 30 40 50

  it=set_union (first, first+5, second, second+5, v.begin());
                                               // 5 10 15 20 25 30 40 50  0  0

  cout << "union has " << int(it - v.begin()) << " elements.\n";

  return 0;
}


 

5、set_intersection:两个序列的交集
原型:
template <class InputIterator1, class InputIterator2, class OutputIterator>  
OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
                                  InputIterator2 first2, InputIterator2 last2,OutputIterator result );
template <class InputIterator1, class InputIterator2,class OutputIterator, class Compare>
OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
                                  InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp );
示例:

// set_intersection 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);                           // 0  0  0  0  0  0  0  0  0  0
  vector<int>::iterator it;

  sort (first,first+5);     //  5 10 15 20 25
  sort (second,second+5);   // 10 20 30 40 50

  it=set_intersection (first, first+5, second, second+5, v.begin());
                                               // 10 20 0  0  0  0  0  0  0  0

  cout << "intersection has " << int(it - v.begin()) << " elements.\n";

  return 0;
}


 

6、set_difference:序列(first1,last1)不在序列(first2,last2)中的元素
原型:
template <class InputIterator1, class InputIterator2, class OutputIterator>  
OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
      InputIterator2 first2, InputIterator2 last2,outputIterator result );
template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>  
OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
                                  InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp );
示例:

// set_difference 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);                           // 0  0  0  0  0  0  0  0  0  0
  vector<int>::iterator it;

  sort (first,first+5);     //  5 10 15 20 25
  sort (second,second+5);   // 10 20 30 40 50

  it=set_difference (first, first+5, second, second+5, v.begin());
                                               // 5 15 25  0  0  0  0  0  0  0

  cout << "difference has " << int(it - v.begin()) << " elements.\n";

  return 0;
}


 

7、set_symmetric_difference:所有不在序列(first1,last1)和序列(first2,last2)中的元素
原型:
template <class InputIterator1, class InputIterator2, class OutputIterator>  
OutputIterator    set_symmetric_difference ( InputIterator1 first1, InputIterator1 last1,
                                             InputIterator2 first2, InputIterator2 last2,OutputIterator result );
template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> 
OutputIterator    set_symmetric_difference ( InputIterator1 first1, InputIterator1 last1, 
                                             InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp );
示例:

// set_symmetric_difference 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);                           // 0  0  0  0  0  0  0  0  0  0
  vector<int>::iterator it;

  sort (first,first+5);     //  5 10 15 20 25
  sort (second,second+5);   // 10 20 30 40 50

  it=set_symmetric_difference (first, first+5, second, second+5, v.begin());
                                               // 5 15 25 30 40 50  0  0  0  0

  cout << "symmetric difference has " << int(it - v.begin()) << " elements.\n";

  return 0;
}






















### C语言中Merge函数的实现与用法 在C语言中,并不存在标准库定义的`merge`函数。然而,在某些特定场景下,开发者可能需要自己实现类似的逻辑来完成数据合并的操作。通常情况下,“merge”的概念更多地应用于数据库操作或者高级编程语言(如Python或R)。以下是基于已知引用以及C语言特性对`merge`功能的理解和实现。 #### 数据结构的选择 为了实现类似于SQL `MERGE INTO`的功能,假设我们有两个数组作为输入源,分别表示目标集合A和源集合B。我们需要通过某种键值比较规则判断两个集合之间的关系并决定如何处理冲突情况: - 如果存在匹配项,则更新该记录; - 若无匹配项,则向目标集中插入新条目。 这里采用简单的整型数组模拟这一过程,实际应用时可根据需求扩展至更复杂的数据类型甚至自定义结构体形式存储更多信息字段。 #### 函数原型设计 考虑到灵活性及可读性,我们将提供如下接口供调用者使用: ```c void merge(int *targetArray, int targetSize, const int *sourceArray, int sourceSize, bool (*compareFunc)(int keyFromTarget, int keyFromSource), void (*updateAction)(int* elementInTarget, int newValue)); ``` 其中参数说明如下: - `targetArray`: 目标数组指针. - `targetSize`: 当前有效长度的目标数组大小. - `sourceArray`: 来源数组常量指针. - `sourceSize`: 来源数组尺寸. - `compareFunc`: 用户提供的回调函数用于指定两组元素间的关系判定准则.[^3] - `updateAction`: 可选动作当发现重复项目时候执行的具体修改行为. 此设计方案允许使用者自由定制比较方式而不局限于数值相等与否; 同样也支持多样化的更新策略而非仅仅覆盖原始值. #### 示例代码展示 下面给出一段具体例子演示上述思路的实际运用效果: ```c #include <stdio.h> #include <stdbool.h> // 定义辅助打印方法便于观察中间状态变化 static inline void print_array(const char* label, const int array[], size_t length){ printf("%s:",label); for (size_t i=0;i<length;++i){ printf(" %d",array[i]); } puts(""); } bool compare_integers(int a,int b){return a==b;} void update_with_addition(int* dest,const int src){(*dest)+=src;} int main(){ // 初始化测试数据 static int destination[] = {1,2,4}; const int d_len = sizeof(destination)/sizeof(*destination); const int sources[] ={2,-3,5}; const int s_len = sizeof(sources)/sizeof(*sources); // 调用通用版本的merge算法 merge(destination,d_len,sources,s_len,&compare_integers,&update_with_addition); // 输出最终结果验证正确性 print_array("After Merge",destination,(size_t)d_len); } ``` 以上程序片段展示了基本框架下的运作机制: 首先准备好了待加工材料即初始态的目的列表还有补充素材;接着引入外部工具箱里的组件——这里是预设好的判别依据加算术运算器组合而成的整体解决方案;最后经过一番折腾之后得到预期产物并通过终端呈现出来加以确认准确性[^4]. 值得注意的是,这里的实现在内存分配方面采取了较为保守的态度,默认不会动态增长目的容器容量。因此对于那些超出预定范围的新成员只能遗憾舍弃掉。如果有更高层次的要求比如自动扩容等功能的话,则需另行考虑其他技术手段予以解决。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值