用C++顺序表或者链表实现交集并集和补集
发布网友
发布时间:2023-03-30 17:20
我来回答
共2个回答
热心网友
时间:2023-11-04 15:10
//Visual Studio 2010 亲测可用 #include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
using namespace std;int main()
{
int a[]={1,5,8,12,5,-5,32};
int b[]={3,5,1,-3,10};
list< int > set1(a,a+sizeof(a)/sizeof(int));
list< int > set2(b,b+sizeof(b)/sizeof(int));
list< int > result;
set1.sort();
set2.sort();
//交集
set_intersection(set1.begin(),set1.end(),set2.begin(),set2.end(),back_inserter(result));
copy(result.begin(),result.end(),ostream_iterator< int >(cout," "));
cout<<endl;
result.clear();
//并集
set_union(set1.begin(),set1.end(),set2.begin(),set2.end(),back_inserter(result));
copy(result.begin(),result.end(),ostream_iterator< int >(cout," "));
cout<<endl;
result.clear();
//补集
set_difference(set1.begin(),set1.end(),set2.begin(),set2.end(),back_inserter(result));
copy(result.begin(),result.end(),ostream_iterator< int >(cout," "));
system("pause");
return 0;
}
热心网友
时间:2023-11-04 15:10
能用到链表的,都不是简单的