1 回答
TA贡献1797条经验 获得超4个赞
#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," "));
return 0;
}
- 1 回答
- 0 关注
- 813 浏览
添加回答
举报