Write the code and make function to find the difference and intersection Example
ID: 3581659 • Letter: W
Question
Write the code and make function to find the difference and intersection
Example:
int arrA[] = {1, 3, 4, 5, 7, 8, 11};
int arrB[] = {2, 4 ,5, 8, 9};
set<int> setA(arrA, arrA+7}, setB(arrB, arrB+5}, setC;
// Union
set C = setA + setB;
writeContainer(setC.begin(), setC.end());
// output: 1 2 3 4 5 7 8 9 11
set C = setA * setB;
writeContainer(setC.begin(), setC.end());
// output: 4 5 8
set C = setA - setB;
writeContainer(setC.begin(), setC.end());
// output: 1 3 7 11
Code for the overloaded Union ‘+’ operator:
#include <iostream>
#include <iterator>
#include <iomanip>
#include <set> // associative container
using namespace std;
template <class Iterator> // Iterator: capital I!
void writeContainer(Iterator first, Iterator last)
{
Iterator iter = first;
while (iter != last)
{
cout << setw(4) << *iter;
iter++;
}
}
template<class T>
set<T> operator+(const set<T> &a, const set<T> &b) // the Union operator+
{
set<T> aUb;
set<T>::iterator itr = a.begin();
while (itr != a.end())
aUb.insert(*itr++);
itr = b.begin();
while (itr != b.end())
aUb.insert(*itr++);
return aUb;
}
int main()
{
int arr1[] = {1, 3, 8, 9, 10};
int arrSize = sizeof(arr1) / sizeof(int);
set<int> aset(arr1, arr1 + arrSize);
cout << "Set A contains: ";
writeContainer(aset.begin(), aset.end());
int arr2[] = {2, 3, 6, 9};
arrSize = sizeof(arr2) / sizeof(int);
set<int> bset(arr2, arr2 + arrSize);
cout << " Set B contains: ";
writeContainer(bset.begin(), bset.end());
cout << endl;
set<int> aUb;
aUb = aset + bset;
cout << " Union of two sets: A U B = ";
writeContainer(aUb.begin(), aUb.end());
cout << endl << endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include <iterator>
#include <iomanip>
#include <set> // associative container
using namespace std;
template <class Iterator> // Iterator: capital I!
void writeContainer(Iterator first, Iterator last)
{
Iterator iter = first;
while (iter != last)
{
cout << setw(4) << *iter;
iter++;
}
}
template<class T>
set<T> operator+(const set<T> &a, const set<T> &b) // the Union operator+
{
set<T> aUb;
set<T>::iterator itr = a.begin();
while (itr != a.end())
aUb.insert(*itr++);
itr = b.begin();
while (itr != b.end())
aUb.insert(*itr++);
return aUb;
}
template<class T>
set<T> operator-(const set<T> &a, const set<T> &b) // the Union operator-
{
set<T> aUb;
set<T>::iterator itr1 = a.begin();
set<T>::iterator itr2 = b.begin();
//loop through size of b as it's size is less
while (itr2 != b.end())
{
if (*itr1 != *itr2)
{
aUb.insert(*itr1);
}
*itr1++;
*itr2++;
}
while (itr1!=a.end())
{
aUb.insert(*itr1++);
}
return aUb;
}
template<class T>
set<T> operator*(const set<T> &a, const set<T> &b) // the Union operator*
{
set<T> aUb;
set<T>::iterator itr1 = a.begin();
set<T>::iterator itr2 = b.begin();
//loop through size of b as it's size is less
while (itr2 != b.end())
{
if (*itr1 == *itr2)
{
aUb.insert(*itr1);
}
*itr1++;
*itr2++;
}
return aUb;
}
int main()
{
int arr1[] = { 1, 3, 8, 9, 10 };
int arrSize = sizeof(arr1) / sizeof(int);
set<int> aset(arr1, arr1 + arrSize);
cout << "Set A contains: ";
writeContainer(aset.begin(), aset.end());
int arr2[] = { 2, 3, 6, 9 };
arrSize = sizeof(arr2) / sizeof(int);
set<int> bset(arr2, arr2 + arrSize);
cout << " Set B contains: ";
writeContainer(bset.begin(), bset.end());
cout << endl;
set<int> aUb;
aUb = aset + bset;
cout << " Union of two sets: A U B = ";
writeContainer(aUb.begin(), aUb.end());
cout << endl << endl;
//test operator -
aUb = aset - bset;
cout << "Difference of two sets: A U B = ";
writeContainer(aUb.begin(), aUb.end());
cout << endl << endl;
//test operator *
aUb = aset * bset;
cout << "Product of two sets: A U B = ";
writeContainer(aUb.begin(), aUb.end());
cout << endl << endl;
return 0;
}
------------------------------------------------------------------------
output
Set A contains: 1 3 8 9 10
Set B contains: 2 3 6 9
Union of two sets: A U B = 1 2 3 6 8 9 10
Difference of two sets: A U B = 1 8 10
Product of two sets: A U B = 3 9
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.