473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

operator() -- not executed inside for_each()

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

Jul 23 '05 #1
6 1534
Michal Wyrebski wrote:
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
Jul 23 '05 #2
Michal Wyrebski wrote:
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
Jul 23 '05 #3

Krishanu Debnath wrote:
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

Jul 23 '05 #4
Michal Wyrebski wrote:
Krishanu Debnath wrote:

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?


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
Jul 23 '05 #5

Krishanu Debnath wrote:
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.


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

Jul 23 '05 #6
Michal Wyrebski wrote:
Krishanu Debnath wrote:
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.

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
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: BCC | last post by:
In looking through some code I have inherited, I notice a lot of places where the programmer used operator() as a function call: void operator() (int x, int y); Rather than an explicit...
2
by: Malcolm Smith | last post by:
I have this functor: struct DeleteIterObject { template< typename T > void operator()(const T* ptr) const { delete ptr; ptr = NULL; }
4
by: Jef Driesen | last post by:
Is there an STL algorithm that does something like for (iterator i = begin(); i != end(); ++i) f(*i,value); where f results in the expression *i += value. I already found the algorithm...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
7
by: Bas Nedermeijer | last post by:
Hello, i am having trouble to use the sort() function of a list<>. I cannot seem to overload the operator<(). The variables to compare are references, like Item* itemA; Item* itemB;
3
by: Nindi | last post by:
On comp.lang.c++.moderated http://groups.google.co.uk/group/comp.lang.c++.moderated/browse_thread/thread/8250715711da7760?hl=en the following question was posted ...
7
by: Deron Meranda | last post by:
This is an optimization problem, one that occurs deep inside a loop that gets executed thousands of times. I'm looking for something in Python which would act like an identity operator, or...
3
by: George2 | last post by:
Hello everyone, I can not find related information in MSDN, so I want to confirm here, that, the implementation of for_each is something like, template <class A, class BB for_each (A...
3
by: C++Liliput | last post by:
Hi, I was looking at the implementation of operator new and operator new in gcc source code and found that the implementation is exactly the same. The only difference is that the size_t argument...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.