1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <iostream>
#include <set>
#include <iterator>
using namespace std;
template <typename T>
set<T> symDifference(const set<T>& setA, const set<T>& setB)
{
set<T> result;
typename set<T>::const_iterator setAiter = setA.begin(), setBiter = setB.begin();
while(setAiter != setA.end())
{
if(setBiter == setB.end())
{
result.insert(*setAiter);
++setAiter;
}
else if(*setAiter < *setBiter)
{
result.insert(*setAiter);
++setAiter;
}
else if(*setAiter > *setBiter)
{
result.insert(*setBiter);
setBiter++;
}
else if(*setAiter == *setBiter)
{
++setAiter;
++setBiter;
}
}
while(setBiter != setB.end())
{
result.insert(*setBiter);
++setBiter;
}
return result;
}
int main() {
int arr[] = {1, 2, 4, 6, 7, 9};
int arrSize = sizeof(arr)/sizeof(int);
set<int> aset(arr,arr+arrSize);
int array[] = {2, 3, 4, 5, 6, 10};
int arraySize = sizeof(array)/sizeof(int);
set<int> bset(array,array+arrSize);
set<int>cset = symDifference(aset,bset);
for(set<int>::iterator iter = cset.begin(); iter != cset.end(); iter++)
{
cout << *iter << ' ';
}
cout << endl << endl;
}
|