Advertisement

set function

Started by September 30, 2023 04:57 PM
25 comments, last by pbivens67 1ย year, 2ย months ago

how do I pass parameters to the set union_and intersection functions in the main function.

this is a exercise in my book. here is my code

#include <iostream>
#include <set>
#include <iterator>

using namespace std;

set <int> set_union(set<int> a, set<int> b)
{
	a.insert(1);
	a.insert(2);
	a.insert(3);
	b.insert(0);
	b.insert(1);
	b.insert(2);
	b.insert(1);
	b.insert(2);

	set<int>::iterator pos;
	for (pos = b.begin(); pos != b.end(); pos++)
	{
		cout << *pos << endl;
	}
	for (pos = a.begin(); pos != a.end(); pos++)
	{
		cout << *pos << endl;
	}
}

set<int> intersection(set<int> a, set<int> b)
{
	a.insert(1);
	a.insert(2);
	a.insert(3);
	b.insert(0);
	b.insert(1);
	b.insert(2);
	a.insert(3);
	b.insert(0);

	set<int>::iterator pos;
	for (pos = a.begin(); pos != a.end(); pos++)
	{
		cout << *pos << endl;
	}
}

int main()
{
	set_union();
	intersection();

	return 0;
}

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.

Advertisement

pbivens67 said:
how do I pass parameters to the set union_and intersection functions in the main function.

Have you tried this?

int main()
{
	set uselssA, uselessB;
	
	set_union(uselssA, uselessB);
	intersection(uselssA, uselessB);

	return 0;
}

It should work? And is just obvious?

However, not sure what you try to do. But i see frob has just explained what looks wrong.

If you try this, the next compiler error you'll get is because you forgot to return stuff from your function.

However, what i dislike the most is this:

set <int> set_union(set<int> a, set<int> b)

It means you give copies of sets a and b, but it can be expensive to make a copy of a container containing many objects.

You could do this instead:

set <int> set_union(set<int> &a, set<int> &b)

Now we give references, so no copies will be created.
However, we now have the risk of our function eventually modifying our original sets, which we might not want.
This can be solved using const correctness:

set <int> set_union(const set<int> &a, const set<int> &b)

Now it's clear to the reader that the function will not modify the data we gave by reference.
If you accidentally try to change the data in the function, the compiler will give you an error. Which then is a helpfull error preventing us from ding bugs.
Same works for pointers too.

Not what you asked for, but it's important to avoid making it a habit to write slow code.

Edit:

One more exercise for you.

How could you avoid to wtite this:

for (pos = b.begin(); pos != b.end(); pos++)
	{
		cout << *pos << endl;
	}

โ€ฆthree times? :P

That's another bad habit, because:
1. You write it 3 times.

2. Later you make it a function, writing it a 4th time and then editing 3 code sections to replace it with a function call.

I know it's just debug code, but you should do it right regardless, to make it a habit to do things right from the start. ; )

this does not work it gives me the following error E0441 argument list for class template "std::set" is missing

nt main()
{
	set uselssA, uselessB;
	
	set_union(uselssA, uselessB);
	intersection(uselssA, uselessB);

	return 0;
}

Correct, his example doesn't have a proper type for the container. The set<> type is a template container. It needs to know what type of thing goes inside the container. In your book example it is a container for type int, so it should be set<int>.

that worked, but I still need to return a value with the function set<int> set_union.

Advertisement

@pbivens67 You can't learn when other people work out your problems for you. ๐Ÿ™‚

I find it strange that they themselves don't understand this. I think some people like to show off their knowledge, which is evident from your last thread, odd that one was. ๐Ÿ™‚

๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚<โ†The tone posse, ready for action.

you are correct sometimes I like hand holding when it comes to new material.

@pbivens67 Do you understand set theory? Do you have any ideas on where you could apply it in your programming?

๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚<โ†The tone posse, ready for action.

Please remember the additional rules of For Beginners forum:

5. If you are a grizzled veteran answering questions, please remember that even you were a neophyte once, and be particularly cognizant of the fact that the audience here may not have the years of experience you do. Try to be as precise, clear and forgiving in your responses as possible.

6. Please avoid derailing topics into tangential discussions of technical minutiae and subtle pros and cons. Topics that drift too far afield of the original posterโ€™s query may be reigned in.

This topic is closed to new replies.

Advertisement