C++集合运算函数总结-merge、set_union、set_difference、set_symmetric_difference、set_intersection

C++集合运算函数总结

集合A,B。(可以使用数组、list、vector)
头文件:#include<algorithm>
前提:两个集合已经有序。
merge() //归并两个序列,元素总个数不变,只是将两个有序序列归并为一个有序序列。
set_union() //实现求集合A,B的并。
set_difference()//实现求集合A,B的差(即A—B)
set_symmetric_difference()//实现求集合A,B的对称差(即(A-B)并(B-A))
set_intersection()//实现求集合A,B交集。
 
//集合运算
//熊八八
//2013-3-15
 
#include<iostream>
#include<stdio.h>
#include<list>
#include<algorithm>            //set_union求并集
using namespace std;
 
template<class T>
void Print(T List)
{
       class T::iterator iter;
       for(iter=List.begin(); iter!=List.end(); iter++)
              printf("%d ", *iter);
       printf("\n");
}
int main()
{
       list<int> List_A;
       list<int> List_B;
       int temp;
       printf("Enter 5 integers into List_A:\n");
       for(int i=0; i<5; i++)
       {
              scanf("%d", &temp);
              List_A.push_back(temp);
       }
       //printf("Enter some integers into List_B:\n");
       for(int i=0; i<5; i++)
       {
              scanf("%d", &temp);
              List_B.push_back(temp);
       }
      
       List_A.sort();
       List_B.sort();
      
       list<int> List_C(10);
       //不能将操作后的结果重新放入List_A或者List_B.如果非要如此,可以设一中间变量List_C,先将结果存储至List_C,然后List_A = List_C
       //merge(List_A.begin(), List_A.end(), List_B.begin(), List_B.end(), List_C.begin()); //合并
       //set_union(List_A.begin(), List_A.end(), List_B.begin(), List_B.end(), List_C.begin());//并集
       //set_difference(List_A.begin(), List_A.end(), List_B.begin(), List_B.end(), List_C.begin());//差集
       //set_symmetric_difference(List_A.begin(), List_A.end(), List_B.begin(), List_B.end(), List_C.begin());//对称差
       set_intersection(List_A.begin(), List_A.end(), List_B.begin(), List_B.end(), List_C.begin());//交集
      
       printf("List_A contains:\n");
       Print(List_A);
       printf("List_B contains:\n");
       Print(List_B);
       printf("List_C contains:\n");
       Print(List_C);
       system("pause");
       return 0;
}

 


set容器使用时这样比较常用吧

            set_intersection(x.begin(), x.end(), y.begin(), y.end(), inserter(z, z.end()));
            ....

<think>好的,我现在需要在Dev C++中解决不支持std::set_union的问题。首先,我得确认Dev C++使用的编译器和STL版本。Dev C++通常搭配的是较旧版本的MinGW GCC,可能不支持某些C++11或更新的特性。用户提到的问题可能与STL实现不完整有关,比如缺少某些算法函数。 接下来,我需要了解std::set_union的功能。这个函数用于合并两个已排序的范围,生成一个有序的并集,结果存储到目标容器中。函数原型大致如下: template< class InputIt1, class InputIt2, class OutputIt > OutputIt set_union( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first ); 如果标准库中没有这个函数,用户可能需要自己实现一个替代版本。首先,检查是否真的缺少这个函数。有时候可能是头文件没有包含,比如需要包含<algorithm>。但用户可能已经确认过,所以考虑其他方法。 替代方案可以是手动实现set_union的逻辑。基本思路是同时遍历两个已排序的输入范围,逐个比较元素,将较小的元素插入结果,处理重复元素,直到两个范围都处理完毕。这类似于归并排序中的合并步骤,但需要处理重复的情况。 另外,用户提到的引用内容中提到了C++的allocator配置器,这可能与问题无关,但需要确保内存管理正确。如果自己实现set_union,需要注意目标容器是否有足够的空间,可能需要使用back_inserter或者预先分配足够的内存。 然后,用户可能需要代码示例来手动实现set_union。例如,编写一个自定义函数,接受两个已排序的容器,返回它们的并集。需要注意的是,输入容器必须已排序,否则结果不正确。 此外,用户可能遇到STL版本兼容性问题。如果Dev C++STL确实缺少某些函数,升级编译器或更换IDE可能是一个长期解决方案。但对于当前问题,手动实现更实际。 最后,需要测试实现的函数是否正确,处理边界情况,例如其中一个容器为空,或者所有元素都相同的情况。</think>在Dev C++中,若发现`std::set_union`不可用,可通过以下步骤实现替代方案: ### 1. 确认环境与兼容性 - **检查编译器版本**:Dev C++默认使用MinGW GCC编译器,若版本过旧可能导致STL功能缺失。通过`g++ -v`查看版本,低于4.8可能部分C++11特性未实现[^2]。 - **包含必要头文件**:确保已包含`<algorithm>`和`<iterator>`。 ### 2. 手动实现set_union逻辑 若确认STL不支持该函数,可自行实现类似功能。核心逻辑为**双指针遍历+归并操作**: ```cpp template<typename InputIt1, typename InputIt2, typename OutputIt> OutputIt custom_set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first) { while (first1 != last1 && first2 != last2) { if (*first1 < *first2) { *d_first++ = *first1++; } else if (*first2 < *first1) { *d_first++ = *first2++; } else { // 相等时取其一 *d_first++ = *first1++; ++first2; } } // 处理剩余元素 while (first1 != last1) *d_first++ = *first1++; while (first2 != last2) *d_first++ = *first2++; return d_first; } ``` ### 3. 使用示例 ```cpp #include <vector> #include <iostream> int main() { std::vector<int> a = {1, 2, 4, 5}; std::vector<int> b = {2, 3, 5, 6}; std::vector<int> result; custom_set_union(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(result)); for (int num : result) { std::cout << num << " "; // 输出: 1 2 3 4 5 6 } return 0; } ``` ### 4. 替代方案与优化 - **使用现有STL组合**:若支持`std::merge`,可先合并后去重,但效率较低: ```cpp std::vector<int> merged; std::merge(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(merged)); merged.erase(std::unique(merged.begin(), merged.end()), merged.end()); ``` - **升级编译器**:安装[TDM-GCC](https://jmeubank.github.io/tdm-gcc/)更新工具链,支持完整C++11特性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值