Advertisement

set function

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

frob said:

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.

was this for me or fleabay.

Fleabay, and a few others who may need the reminder.

It is generally a soft rule, but it has proven very important over the decades. The For Beginners area has always needed some additional restrictions to keep it focused.

Any other issues understanding the code from the book? You already wrote that the functions need to return a container, and with that the code should build and run enough to play with.

Advertisement

frob said:
Fleabay, and a few others who may need the reminder.

A person needs to understand the underlying concepts of std containers and why they may be of use before they actually go about learning the implementation of the container.

It seems that you, among others, are the ones that need the reminders. I don't see how you think the rules apply to me and not you. I'm not the one that's breaking them. It's hypocrisy at its finest.

Phil, could you please answer my inquiries and not let Frob derail further?

…Preparing for an unearnd deletion or mod action… request some other mod review this post other than Frob, who has a vested interest.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

pbivens67 said:
that worked, but I still need to return a value with the function set set_union.

Of coarse. But now it's not clear if you have a related question. You did not ask one. Thus i think:
Probably he has a question / problem. But which?
Maybe he does not know how to return a new set created in the function?
Or maybe he dos not know how to calculate a set which is the union or intersection of two given sets?

Notice at this point i'm left with too many questions. I might pick one and answer, but i might pick the wrong question and just waste time. Or i might answer all potential questions i can think of, again wasting time.
Thus, you'll likely get no further help at this point. Instead, all you get is random noise from some pit bull dog coming by, unable to resist pissing at every tree he comes across. Which is not your fault, but still.

So what's your problem, and which question describes this problem?

my question is how do I return a value from my functions.

// Function that increments an integer value and returns the incremented value.
int inc(int value) {
  int result = value + 1; // Compute the result of the function using the supplied 'value' parameter.
  return result; // And return the result.
}

Functions returning a set use the same idea, except “result” should be a set then, of course:

// Function that returns a set with the given value in it.
set<int> make_singleton(int value) {
  set<int> result;
  result.insert(value);
  return result;
}

You use such a function like

set<int> answer; // Make a variable that will store the answer of the function.
answer = make_singleton(37); // Assign the result of the function to the variable.

// You can also combine the above two lines into
set<int> answer = make_singleton(37);
// Then do something with the returned answer.

Advertisement

@pbivens67 Please don't take this as rude, but I think you need to set aside the standard library functions and focus on the basics. Returning a value from a function is one of the most basic C/C++ features taught. As in first day of class-level basic. Whatever book you're using is not the best for beginners like yourself. You need one that delves into the very basics of the language. The standard library comes later.

No, I am not a professional programmer. I'm just a hobbyist having fun...

I know how to return from a simple function, but I am working on how to do that using set.

I worked through my problem and solved it, thx all

pbivens67 said:

I know how to return from a simple function, but I am working on how to do that using set.

I assume you felt uncertain because returning a single int feels simple, but returning an entire container of many objects feels complex and so there might be some difference?
But seems you have figured out the latter indeed just works.

However, there is another way to get data from a function, usually used if we need more than one result:

void ComputeUnionAndIntersection ( // no return
	set<int> &union, set<int> &intersection, // outputs (becasue it's references, we can change the objects in the function and the caller will see those changes)
	set<int> a, set<int> b) // inputs
{
	//...
};

I guess you know that?
But maybe not, because then you could have used this to avoid the uncertainty. It's important. I think i do this as often as returning a single value.

This is also where const correctness becomes so helpful:

void ComputeUnionAndIntersection (
	set<int> &union, set<int> &intersection,
	const set<int> &a, const set<int> &b)
{
	//...
};

Because now i can see which parameters are meant as inputs or outputs, to some degree at least.
No comments needed, and i made the inputs references too to avoid the copy, and it's still not confusingly looking like outputs.

But that just said for your info. Const correctness is purely optional, a matter of taste and not important.

This topic is closed to new replies.

Advertisement