提交时间:2025-03-26 20:12:54

运行 ID: 8163

#include <bits/stdc++.h> using namespace std; int m,n,l; int main(){ vector<int> a; vector<int> b; vector<int> c; cin>>n; for (int i = 0;i<n;i++){ cin>>l; a.push_back(l); } cin>>m; for (int i = 0;i<m;i++){ cin>>l; b.push_back(l); } sort(a.begin(),a.end()); sort(b.begin(),b.end()); set_intersection(a.begin(), a.end(), b.begin(), b.end(), back_inserter(c));//交集 for(vector<int>::iterator it = c.begin(); it != c.end();it ++) cout << *it << " "; cout<<endl; c.clear(); set_union(a.begin(), a.end(), b.begin(), b.end(), back_inserter(c));//并集 for(vector<int>::iterator it = c.begin(); it != c.end();it ++) cout << *it << " "; cout<<endl; c.clear(); set_difference(a.begin(), a.end(), b.begin(), b.end(), back_inserter(c)); //差集(b中属于a的元素去掉) for(vector<int>::iterator it = c.begin(); it != c.end();it ++) cout << *it << " "; cout<<endl; c.clear(); }