#include <iostream>
#include <fstream>
#include <set>
using namespace std;
//求二个集合A、B交集的补集,类似于异或操作
void f(const set<int> &A,const set<int> &B,set<int> &C)
{
C = A;
//用于存放插入元素是否成功
pair<set<int>::iterator,bool> ele;
for(set<int>::const_iterator iter=B.begin(); iter != B.end();++iter)
{
ele = C.insert(*iter);
if(!ele.second)
{
C.erase(*iter);
}
}
}
//测试用例
void main()
{
set<int> a;
set<int> b;
set<int> c;
a.insert(1);a.insert(2);a.insert(2);a.insert(3);a.insert(4);
b.insert(2);b.insert(3);b.insert(7);b.insert(6);b.insert(5);
for(set<int>::const_iterator i = a.begin(); i != a.end(); ++i)
{
cout << *i << " ";
}
cout << endl;
for(set<int>::const_iterator i = b.begin(); i != b.end(); ++i)
{
cout << *i << " ";
}
cout << endl;
f(a,b,c);//将两个集合进行异或操作
for(set<int>::const_iterator i = c.begin(); i != c.end(); ++i)
{
cout << *i << " ";
}
cout << endl;
}
常见面试题_自我陶醉
最新推荐文章于 2025-04-03 15:49:05 发布