operator() -- not executed inside for_each()

  • Thread starter Michal Wyrebski
  • Start date
M

Michal Wyrebski

Hello,

I'm new in this group and I don't know if my questions are too silly,
but I'm intermediate programmer and don't have enought experience.
Please be charitable.

I don't know how to write an operator() class to be properly executed
inside for_each()?

Maybe example will be more accurate:

#v+
#include <algorithm>
#include <iostream>
#include <list>

class Class1
{
private:
/* ... */
double last_value;
public:
Class1() : last_value(0) {}

double show_result() {
return last_value;
}

void operator()(double n) {
if (n > last_value)
last_value = n;
}
}; // class

using namespace std;

int main() {
Class1 functor;
list<int> List1;

for (int i=0; i<6; ++i)
List1.push_back(i);

for_each(List1.begin(), List1.end(), functor);

functor.show_result(); // (1)

return 0;
}
#v-

And the problem is, that (1) returns 0, and I want it to return 5.
Where is a mistake in my reasoning?
How should it be written?

Thanks,


Michal
 
V

Victor Bazarov

Michal said:
I'm new in this group and I don't know if my questions are too silly,
but I'm intermediate programmer and don't have enought experience.
Please be charitable.

I don't know how to write an operator() class to be properly executed
inside for_each()?

Maybe example will be more accurate:

#v+
#include <algorithm>
#include <iostream>
#include <list>

class Class1
{
private:
/* ... */
double last_value;
public:
Class1() : last_value(0) {}

double show_result() {
return last_value;
}

void operator()(double n) {
if (n > last_value)
last_value = n;
}
}; // class

using namespace std;

int main() {
Class1 functor;
list<int> List1;

for (int i=0; i<6; ++i)
List1.push_back(i);

for_each(List1.begin(), List1.end(), functor);

functor.show_result(); // (1)

return 0;
}
#v-

And the problem is, that (1) returns 0, and I want it to return 5.
Where is a mistake in my reasoning?

'for_each' makes a copy of the functor.
How should it be written?

You need some kind of indirection.

class Class1
{
private:
/* ... */
double& last_value; /// reference!!!
public:
Class1(double& d) : last_value(d) {}

double show_result() {
return last_value;
}

void operator()(double n) {
if (n > last_value)
last_value = n;
}
}; // class
...
double a = 0;
Class1 functor(a);
for_each(List1.begin(), List1.end(), functor);

Now, 'functor' keeps the reference to the place where it stores the
last value, and the reference survives the copying of 'functor'.

V
 
K

Krishanu Debnath

Michal said:
Hello,

I'm new in this group and I don't know if my questions are too silly,
but I'm intermediate programmer and don't have enought experience.
Please be charitable.

I don't know how to write an operator() class to be properly executed
inside for_each()?

Maybe example will be more accurate:

#v+
#include <algorithm>
#include <iostream>
#include <list>

class Class1
{
private:
/* ... */
double last_value;
public:
Class1() : last_value(0) {}

double show_result() {
return last_value;
}

void operator()(double n) {
if (n > last_value)
last_value = n;
}
}; // class

using namespace std;

int main() {
Class1 functor;
list<int> List1;

for (int i=0; i<6; ++i)
List1.push_back(i);

for_each(List1.begin(), List1.end(), functor);

functor.show_result(); // (1)

return 0;
}
#v-

And the problem is, that (1) returns 0, and I want it to return 5.
Where is a mistake in my reasoning?
How should it be written?

Thanks,


Michal

You wrote functor pretty well. But the last argument of for_each is passed as value. So you cannot
expect the change in main. You need to capture the value that for_each returns.

write ::
functor = for_each(List1.begin(), List1.end(), functor);

Krishanu
 
M

Michal Wyrebski

Krishanu said:
write ::
functor = for_each(List1.begin(), List1.end(), functor);

This works very well, but I have some doubt.

Does this solutions work ever, even if operator() is overloaded?
Could for_each() work on several copies of functor?
What if some elements are not maintained by one copy of functor?

Solution provided by Victor Bazarov is great and also may be used, but
in my case it's undesirable to have another variable (double a;).

Thanks.


Michal
 
K

Krishanu Debnath

Michal said:
Krishanu Debnath wrote:




This works very well, but I have some doubt.

Does this solutions work ever, even if operator() is overloaded?

Yes, as long as one of your () operator suitable match with your container's
element type.
Could for_each() work on several copies of functor?
What if some elements are not maintained by one copy of functor?

I didn't understand your above two questions.

Krishanu
 
M

Michal Wyrebski

Krishanu said:
Yes, as long as one of your () operator suitable match with your container's
element type.
I didn't understand your above two questions.

Neah, sorry. I'm semiconscious today.

Now those questions after a mug of coffee:

Is it possible that for_each() works on several copies of functor, ex.
operator() is overloaded, and for_each() provides several types of data
to functor, so for_each() creates several copies of functor, and we
don't know which would be returned.

Someone's told me that it is not told that all elements will be
maintained by the same copy of functor.

And I'm not convinced about it, because how elements of list could be
more than one type when we have to declare the type of list.


Michal
 
K

Krishanu Debnath

Michal said:
Neah, sorry. I'm semiconscious today.

Now those questions after a mug of coffee:

Is it possible that for_each() works on several copies of functor, ex.
operator() is overloaded, and for_each() provides several types of data
to functor, so for_each() creates several copies of functor, and we

No, in your hypothetical situtation for_each can call different version of
operator(). There is *exactly one functor object* in for_each function.

But this sort of situtation cannot arise, as container can contain only one
type of data.

Krishanu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top