C语言程序整数集合的差集与交集
2013-11-28
展开全部
#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.ort();
//交集
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.ort();
//交集
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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询