Hi! Here is a simple program: #include <iostream> #include <algorithm> #include <functional> #include <stdexcept> using namespace std; #include <ginac/ginac.h> using namespace GiNaC; class lst_prod { ex prod; public: lst_prod() : prod(ex(1)) { } lst_prod(const ex & init_) : prod(init_) { } void operator()(const ex & x) { cout << "We are called with argument " << x << endl; prod *= x; cout << "Current value of product is " << prod << endl; } ex result() const { return prod; } }; int main() { symbol a("a"), b("b"), c("c"); lst l; l = a, b, c; lst_prod test; for_each(l.begin(), l.end(), test); ex res = test.result(); cout << "---------------------"; cout << "List is " << l << endl; cout << "Product of it's elements is " << res << endl; return 0; } I expected res to be a*b*c, but the output is: We are called with argument a Current value of product is a We are called with argument b Current value of product is b*a We are called with argument c Current value of product is c*b*a --------------------- List is {a,b,c} Product of it's elements is 1 This looks like a bug or a very strange feature...
Hi! On Sun, Jan 25, 2004 at 01:19:51PM +0300, Sheplyakov Alexei wrote:
lst_prod test; for_each(l.begin(), l.end(), test);
The functor used by for_each() is a copy of "test". Try this instead: lst_prod test = for_each(l.begin(), l.end(), lst_prod()); Bye, Christian -- / Physics is an algorithm \/ http://www.uni-mainz.de/~bauec002/
Hi!
lst_prod test; for_each(l.begin(), l.end(), test);
The functor used by for_each() is a copy of "test".
Yes... Looks like I need [more] RTFM...
Try this instead:
lst_prod test = for_each(l.begin(), l.end(), lst_prod());
Thanks, it works as expected. Sorry for a silly question...
participants (2)
-
Christian Bauer
-
varg@thsun1.jinr.ru