# Problem F: STL——集合运算

Problem F: STL——集合运算
Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 4569  Solved: 2209
Description

A∪B={x|x∈A∨x∈B}
A∩B={x|x∈A∧x∈B}
A-B={x|x∈A∧x不属于 B}
SA ={x|x∈(A∪B)∧x 不属于A}
SB ={x|x∈(A∪B)∧x 不属于B}
Input

Output

Sample Input
14 1 2 3 10
Sample Output
Case# 1:A = {1, 2, 3}B = {}A u B = {1, 2, 3}A n B = {}A - B = {1, 2, 3}SA = {}SB = {1, 2, 3}
HINT

Append Code

#include <iostream>
#include <cstring>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{

int n,i;
cin>>n;
for(i=0;i<n;i++)
{set<int> A;
set<int> B;
set<int> tm1,tm2,tm3,tm4,tm5;
cout<<"Case# "<<i+1<<":"<<endl;
int x,j=0;
cin>>x;
for(j=0;j<x;j++)
{int a;cin>>a;A.insert(a);}
int y,k=0;
cin>>y;
for(k=0;k<y;k++)
{int b;cin>>b;B.insert(b);}
// int st1,st2,ed1,ed2;
// strlen(A)=st1;
// cout<<st1;
set<int>::iterator itA; //定义前向迭代器
cout<<"A = {";
for(itA=A.begin();itA!=A.end();itA++)//中序遍历集合中的所有元素
{
if(itA==A.begin()){cout<<*itA;}
else cout<<", "<<*itA;
}
cout<<"}"<<endl;

set<int>::iterator itB; //定义前向迭代器
cout<<"B = {";
for(itB=B.begin();itB!=B.end();itB++)//中序遍历集合中的所有元素
{
if(itB==B.begin()){cout<<*itB;}
else cout<<", "<<*itB;
}
cout<<"}"<<endl;

set_union(A.begin(), A.end(), B.begin(), B.end(), inserter(tm1, tm1.begin()));
set<int>::iterator ittm1; //定义前向迭代器
cout<<"A u B = {";
for(ittm1=tm1.begin();ittm1!=tm1.end();ittm1++)//中序遍历集合中的所有元素
{
if(ittm1==tm1.begin()){cout<<*ittm1;}
else cout<<", "<<*ittm1;
}
cout<<"}"<<endl;

set<int>::iterator ittm2;
set_intersection(A.begin(), A.end(), B.begin(), B.end(), inserter(tm2, tm2.begin()));
cout<<"A n B = {";
for(ittm2=tm2.begin();ittm2!=tm2.end();ittm2++)//中序遍历集合中的所有元素
{
if(ittm2==tm2.begin()){cout<<*ittm2;}
else cout<<", "<<*ittm2;
}
cout<<"}"<<endl;

set<int>::iterator ittm3;
set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(tm3, tm3.begin()));
cout<<"A - B = {";
for(ittm3=tm3.begin();ittm3!=tm3.end();ittm3++)//中序遍历集合中的所有元素
{
if(ittm3==tm3.begin()){cout<<*ittm3;}
else cout<<", "<<*ittm3;
}
cout<<"}"<<endl;

set<int>::iterator ittm4;
set_difference(tm1.begin(), tm1.end(), A.begin(), A.end(), inserter(tm4, tm4.begin()));
cout<<"SA = {";
for(ittm4=tm4.begin();ittm4!=tm4.end();ittm4++)//中序遍历集合中的所有元素
{
if(ittm4==tm4.begin()){cout<<*ittm4;}
else cout<<", "<<*ittm4;
}
cout<<"}"<<endl;

set<int>::iterator ittm5;
set_difference(tm1.begin(), tm1.end(), B.begin(), B.end(), inserter(tm5, tm5.begin()));
cout<<"SB = {";
for(ittm5=tm5.begin();ittm5!=tm5.end();ittm5++)//中序遍历集合中的所有元素
{
if(ittm5==tm5.begin()){cout<<*ittm5;}
else cout<<", "<<*ittm5;
}
cout<<"}"<<endl;

}
// A.clear();B.clear();
// tm1.clear();tm2.clear();tm3.clear();tm4.clear();tm5.clear();

return 0;
}

2
1 2
3 2 2 1
4 1 2 3 1
0