For working through your book, and since this isn’t a homework assignment:
Remember that you are working with containers.
You want to fill up the containers in your main()
function, and pass them along to the functions.
In your function definitions you start with: set
That is, the line says "This function returns a set
The names a and b are only used inside the function, it doesn’t matter what those containers are called elsewhere.
All those a.insert()
and b.insert()
aren’t part of the work of computing a union or intersection, so they don’t belong in the functions. Instead, those belong in main()
. And since they don’t need to be called a and b anywhere else, it’s good to give them useful names.
So you might have something like this in main instead:
int main()
{
set SomeNumbers; // Create a container
set OtherNumbers; // Create another container
SomeNumbers.insert(1); // Add to the first container
SomeNumbers.insert(2);
SomeNumbers.insert(3);
OtherNumbers.insert(0); // Add to the second container
OtherNumbers.insert(1);
OtherNumbers.insert(2);
...
Then you can send those collections to your functions:
...
set_union( SomeNumbers, OtherNumbers);
intersection( SomeNumbers, OtherNumbers);
...
You could list any two set
collections you have created and it will work on them.
But wait, let’s look deeper.
The functions also say they have a return value. You’ve got set
They say they will return a set
However, the code you posted doesn’t actually return a set, the functions set_union()
and intersection()
end without returning anything, even though their signature says on the left of their name that they return set
If those signatures come from your book, you are probably meant to create a set
inside the function, fill it up, and return it. I think it best that you struggle over that, rather than posting it here for you. In short form, your function would create a container inside the function, add things to the container, then return the container.
Your code probably won’t compile until you do the first and last; your function needs to create a set
variable inside it, and then return that variable at the end.
Then you can capture the result if you want, back in main()
:
...
set NumberUnion = set_union( SomeNumbers, OtherNumbers);
set NumberIntersection = intersection( SomeNumbers, OtherNumbers);
...
And then you can do additional processing with those new collections.
Credit goes to the respective owner!