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 <int> set_union(set<int> a, set<int> b)
That is, the line says "This function returns a set<int>, it is called set_union, and it accepts a parameter of type set<int> I will call a, and a parameter of type set<int> I will call b."
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<int> SomeNumbers; // Create a container
set<int> 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<int>
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 <int> set_union(set<int> a, set<int> b)
They say they will return a set<int>, that's the part before the function name.
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<int>. So that's something you'll need to fix.
If those signatures come from your book, you are probably meant to create a set<int>
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<int>
variable inside it, and then return that variable at the end.
Then you can capture the result if you want, back in main()
:
...
set<int> NumberUnion = set_union( SomeNumbers, OtherNumbers);
set<int> NumberIntersection = intersection( SomeNumbers, OtherNumbers);
...
And then you can do additional processing with those new collections.