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