用链表实现交集并集
答案:1 悬赏:70 手机版
解决时间 2021-04-17 00:39
- 提问者网友:浩歌待明月
- 2021-04-16 06:50
用链表实现交集并集
最佳答案
- 五星知识达人网友:归鹤鸣
- 2021-04-16 07:41
#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;
}
#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;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯