I need someone to explain to me how to implement Intersection and Difference in a Set. I know how to do the Union but I'm stumped on the other two. I also need help finding a certain number in the set (under isThere).
Thank you in advance.
Set Set::Union(const Set &setB) const
{
Set setNew(items);
Set tempSet;
for (int i = 0; i < setB.items.size(); i++)
{
if (setNew.IsThere(setB.items[i]) == false)
{
tempSet.items.push_back(setB.items[i]);
}
}
setNew.items.insert(setNew.items.end(), tempSet.items.begin(), tempSet.items.end());
return setNew;
}
Set Set::Intersection(const Set &setB) const
{
Set setNew;
return setNew;
}
Set Set::Difference(const Set &setB) const
{
Set setNew;
return setNew;
}
bool Set::IsThere(int item) const
{
bool numFound = false;
return numFound;
}
you need an efficient way to know whether an item is in a set ... like a binary search, or if allowed, make the core of your set class an unordered_map. If you did not, it may be wise to ensure that an item can only be put in the set once.
that said, intersection is
for everything in this set
if it is also in the other set
add to new set
and difference is
for everything in this set
if it is not in the other set
add to new set
so that implies that 'isthere' needs to be written first, but again, if you use the map, its easy. If you do something else, you have to implement some kind of search, preferably the search needs to be very efficient or large sets will bog down into N*N work when doing the intersection/difference routines.
@OP You have a lot of problems but these two changes including the one mentioned by @jonnin will get you moving:
Keep it really simple and don't worry about binary searches and maps because you won't need them
Set Union(const Set &setB) const
{
Set new_set;
new_set.items = setB.items;
int element;
for (int i = 0; i < this->items.size(); i++)
{
element = this->items[i];
if (new_set.IsThere(element) == false)
{
new_set.items.push_back(element);
}
}
return new_set;
}
Set Intersection(const Set &setB) const
{
Set new_set;
int element;
for (int i = 0; i < this->items.size(); i++)
{
element = this->items[i];
if (setB.IsThere(element) == true)
{
new_set.items.push_back(element);
}
}
return new_set;
}
Member(s) in setA: 1 3 5 7 9
Member count: 5
============================================
Member(s) in setB: 1 13 5
Member count: 3
============================================
Member(s) in setC:
Member count: 0
============================================
===== Testing IsThere Member Function =====
5 is in setA: 1
20 is in setA: 0
============================================
Member(s) in setU_AB (A U B): 1 13 5 3 7 9
Member count: 6
============================================
Member(s) in setU_BA (B U A): 1 3 5 7 9 13
Member count: 6
============================================
Member(s) in setI_AB (A intersection B): 1 5
Member count: 2
============================================
Member(s) in setI_BA (B intersection A): 1 5
Member count: 2
============================================
Member(s) in setD_AB (A - B): 3 7 9
Member count: 3
============================================
Member(s) in setD_BA (B - A): 13
Member count: 1
============================================
Program ended with exit code: 0
Oops wrong <>tag now fixed. BTW I commented out .Add hence strange result