Connecting Tech Pros Worldwide Forums | Help | Site Map

operator() -- not executed inside for_each()

Michal Wyrebski
Guest
 
Posts: n/a
#1: Jul 23 '05
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


Krishanu Debnath
Guest
 
Posts: n/a
#2: Jul 23 '05

re: operator() -- not executed inside for_each()


Michal Wyrebski wrote:[color=blue]
> 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[/color]

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
Victor Bazarov
Guest
 
Posts: n/a
#3: Jul 23 '05

re: operator() -- not executed inside for_each()


Michal Wyrebski wrote:[color=blue]
> 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?[/color]

'for_each' makes a copy of the functor.
[color=blue]
> How should it be written?[/color]

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
Michal Wyrebski
Guest
 
Posts: n/a
#4: Jul 23 '05

re: operator() -- not executed inside for_each()



Krishanu Debnath wrote:
[color=blue]
> write ::
> functor = for_each(List1.begin(), List1.end(), functor);
>[/color]

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

Krishanu Debnath
Guest
 
Posts: n/a
#5: Jul 23 '05

re: operator() -- not executed inside for_each()


Michal Wyrebski wrote:[color=blue]
> Krishanu Debnath wrote:
>
>[color=green]
>>write ::
>>functor = for_each(List1.begin(), List1.end(), functor);
>>[/color]
>
>
> This works very well, but I have some doubt.
>
> Does this solutions work ever, even if operator() is overloaded?[/color]

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

I didn't understand your above two questions.

Krishanu
Michal Wyrebski
Guest
 
Posts: n/a
#6: Jul 23 '05

re: operator() -- not executed inside for_each()



Krishanu Debnath wrote:[color=blue]
> Yes, as long as one of your () operator suitable match with your container's
> element type.[/color]
[color=blue][color=green]
> > Could for_each() work on several copies of functor?
> > What if some elements are not maintained by one copy of functor?[/color][/color]
[color=blue]
> I didn't understand your above two questions.[/color]

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

Krishanu Debnath
Guest
 
Posts: n/a
#7: Jul 23 '05

re: operator() -- not executed inside for_each()


Michal Wyrebski wrote:[color=blue]
> Krishanu Debnath wrote:
>[color=green]
>>Yes, as long as one of your () operator suitable match with your container's
>>element type.[/color]
>
>[color=green][color=darkred]
>>>Could for_each() work on several copies of functor?
>>>What if some elements are not maintained by one copy of functor?[/color][/color]
>
>[color=green]
>>I didn't understand your above two questions.[/color]
>
>
> 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[/color]

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
Closed Thread