标准模板3
#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
#include<iterator>
using namespace std;
int main()
{
/*set<int>A;
set<int>B;
set<int>C1;
set<int>C2;
set<int>C3;
set<int>::iterator pos;
int m, n, t;
cout << "请输入第一个集合的元素个数:";
cin >> m;
cout << "输入第一个集合:";
for (int i = 0; i < m; i++)
{
cin >> t;
A.insert(t);
}
cout << endl;
cout << "请输入第二个集合的元素个数:";
cin >> n;
cout << "输入第二个集合:";
for (int i = 0; i < n; i++)
{
cin >> t;
B.insert(t);
}
cout<<endl;
cout << "A={";
for (pos = A.begin(); pos != A.end(); pos++)
{
if (pos != A.begin())
cout << ",";
cout << *pos;
}
cout << "}";
cout << endl;
cout << "B={";
for (pos = B.begin(); pos != B.end(); pos++)
{
if (pos != B.begin())
cout << ",";
cout << *pos;
}
cout << "}";
cout << endl;
set_union(A.begin(), A.end(), B.begin(), B.end(), inserter(C1, C1.begin()));
cout << "C1={";
for (pos = C1.begin(); pos != C1.end(); pos++) //使用迭代器访问值
{
if (pos != C1.begin())
cout << ",";
cout << *pos;
}
cout << "}";
cout << endl;
set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(C2, C2.begin()));
cout << "C2={";
for (pos = C2.begin(); pos != C2.end(); pos++) //使用迭代器访问值
{
if (pos != C2.begin())
cout << ",";
cout << *pos;
}
cout << "}";
cout << endl;
set_intersection(A.begin(), A.end(), B.begin(), B.end(), inserter(C3, C3.begin()));
cout << "C3={";
for (pos = C3.begin(); pos != C3.end(); pos++) //使用迭代器访问值
{
if (pos != C3.begin())
cout << ",";
cout << *pos;
}
cout << "}";
cout << endl;*/
int a[] = { 1,3,2,5,6,4,9,8,7,10};
int b[] = { 18,14,2,12,6,8,0,4,10,16};
int c[] = { 100 };
int d[] = { 100 };
int e[] = { 100 };
vector<int>A(a, a + sizeof(a) / sizeof(int));
sort(A.begin(), A.end());//排序
copy(A.begin(), A.end(), ostream_iterator<int>(cout,","));
cout << endl; cout << endl;
vector<int>B(b, b + sizeof(b) / sizeof(int));
sort(B.begin(), B.end());
copy(B.begin(), B.end(), ostream_iterator<int>(cout, ","));
cout << endl;
vector<int>C(c, c + sizeof(c) / sizeof(int));
vector<int>::iterator pos;
set_union(A.begin(), A.end(), B.begin(), B.end(),inserter(C, C.begin()));
cout << "C={";
for (pos = C.begin(); pos != C.end(); pos++) //使用迭代器访问值
{
if (pos != C.begin())
cout << ",";
cout << *pos;
}
cout << "}";
cout << endl;
vector<int>D(d, d + sizeof(d) / sizeof(int));
set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(D, D.begin()));
cout << "D={";
for (pos = D.begin(); pos != D.end(); pos++) //使用迭代器访问值
{
if (pos != D.begin())
cout << ",";
cout << *pos;
}
cout << "}";
cout << endl;
vector<int>E(e, e + sizeof(e) / sizeof(int));
set_intersection(A.begin(), A.end(), B.begin(), B.end(), inserter(E, E.begin()));
cout << "E={";
for (pos = E.begin(); pos != E.end(); pos++) //使用迭代器访问值
{
if (pos != E.begin())
cout << ",";
cout << *pos;
}
cout << "}";
cout << endl;
return 0;
}