How can I save the values in lambda expression? Can any one with kindness help me?
I am using Dev-C++ with boost lambda expression, I wrote:
....................
int nnumber;
int switchvalue;
typedef std::map
AMDG fmingu wrote:
I am using Dev-C++ with boost lambda expression, I wrote: .................... int nnumber; int switchvalue; typedef std::map
IIPrimemap; IIPrimemap primemapvec; # define BetaKGre2 1 # define EqualToOne 2 #define ProductOfKPrime 3 ..................... if_then_else(var(nnumber)==1,var(switchvalue)=EqualToOne, if_then_else(constant(count_if(primemapvec.begin(),primemapvec.end(), bind(&IIPrimemap::value_type::second,_1)>2)>0), var(switchvalue)=BetaKGre2,var(switchvalue)=ProductOfKPrime)); std::cout<<" the var(switchvalue) is "< without any expression of var(switchvalue). I do not know how to save the values in lambda expression and where is wrong? Can I use *var(switchvalue) instead of var(switchvalue)? Can anyone with kindness help me?
Think about it. What exactly is var(switchvalue)? What does std::cout << var(switchvalue) do? In Christ, Steven Watanabe
As you know, I am a Chinese and according to my knowledge few book in Chinese introduce boost library. I only have the book " Beyond the C++ Standard Library: An introduction to boost" (written by Bjoern Karlsson, translated in Chinese).Only several pages said about lambda. I tried to ask www.csdn.net( a Chinese developers' net) and few people answered my questions. I have not studied the source code of lambda yet. So all I have to do is to ask www.boost.org to get answers and go forth.
I think the var(switchvalue) is a pointer to a function from the example code in the book:
..........
std::for_each(vec.begin(),vec.end(),var(m)=_1);
..............
So the operations in m can be performed. Am I right?
But I do not know how to save the values in lambda expression.
Thanks a lot.
在2009-07-09,"Steven Watanabe"
AMDG
fmingu wrote:
I am using Dev-C++ with boost lambda expression, I wrote: .................... int nnumber; int switchvalue; typedef std::map
IIPrimemap; IIPrimemap primemapvec; # define BetaKGre2 1 # define EqualToOne 2 #define ProductOfKPrime 3 ..................... if_then_else(var(nnumber)==1,var(switchvalue)=EqualToOne, if_then_else(constant(count_if(primemapvec.begin(),primemapvec.end(), bind(&IIPrimemap::value_type::second,_1)>2)>0), var(switchvalue)=BetaKGre2,var(switchvalue)=ProductOfKPrime)); std::cout<<" the var(switchvalue) is "< without any expression of var(switchvalue). I do not know how to save the values in lambda expression and where is wrong? Can I use *var(switchvalue) instead of var(switchvalue)? Can anyone with kindness help me?
Think about it. What exactly is var(switchvalue)? What does std::cout << var(switchvalue) do?
In Christ, Steven Watanabe
2009/7/8 fmingu
As you know, I am a Chinese and according to my knowledge few book in Chinese introduce boost library. I only have the book " Beyond the C++ Standard Library: An introduction to boost" (written by Bjoern Karlsson, translated in Chinese).Only several pages said about lambda. I tried to ask www.csdn.net( a Chinese developers' net) and few people answered my questions. I have not studied the source code of lambda yet. So all I have to do is to ask www.boost.org to get answers and go forth. I think the var(switchvalue) is a pointer to a function from the example code in the book: .......... std::for_each(vec.begin(),vec.end(),var(m)=_1); .............. So the operations in m can be performed. Am I right? But I do not know how to save the values in lambda expression. Thanks a lot.
Try to do only std::cout << switchvalue << std::endl; The value has already been saved, there is no need to use var outside of the lambda expression. for example, in the for_each code you posted, the reason to use var is because std::for_each(vec.begin(),vec.end(), m=_1); is not legal. However, using var it will basically have the same effect. Once the lambda function has executed, m directly contains the updated value and you can access it like any variable.
AMDG fmingu wrote:
As you know, I am a Chinese
As a matter of fact I didn't know.
and according to my knowledge few book in Chinese introduce boost library. I only have the book " Beyond the C++ Standard Library: An introduction to boost" (written by Bjoern Karlsson, translated in Chinese).Only several pages said about lambda. I tried to ask www.csdn.net( a Chinese developers' net) and few people answered my questions. I have not studied the source code of lambda yet.
The official documentation is at http://www.boost.org/libs/lambda. I believe that there is also an Chinese translation project which you might want to check. http://code.google.com/p/boost-doc-zh/
So all I have to do is to ask www.boost.org to get answers and go forth. I think the var(switchvalue) is a pointer to a function from the example code in the book:
var(switchvalue) actually returns a function object which returns a reference to switchvalue. A function pointer wouldn't work for numerous reasons. The point I was trying to make was that std::cout << var(switchvalue) doesn't actually do anything. It returns another function object. So, for example, this will work (std::cout << var(switchvalue))(); and will print the value of switchvalue. Not that you'd actually want to write code like that. The correct way is to leave off the var as Zachary noted.
.......... std::for_each(vec.begin(),vec.end(),var(m)=_1); .............. So the operations in m can be performed. Am I right? But I do not know how to save the values in lambda expression.
In Christ, Steven Watanabe
After your advice,I wrote the following test program:
int main(int argc, char *argv[])
{
int nnumber=7;
int switchvalue=1;
if_then_else(var(nnumber)==6, var(switchvalue)=3
,var(switchvalue)=30);
std::cout<<" the nnumber is "<
AMDG
fmingu wrote:
As you know, I am a Chinese
As a matter of fact I didn't know.
and according to my knowledge few book in Chinese introduce boost library. I only have the book " Beyond the C++ Standard Library: An introduction to boost" (written by Bjoern Karlsson, translated in Chinese).Only several pages said about lambda. I tried to ask www.csdn.net( a Chinese developers' net) and few people answered my questions. I have not studied the source code of lambda yet.
The official documentation is at http://www.boost.org/libs/lambda. I believe that there is also an Chinese translation project which you might want to check. http://code.google.com/p/boost-doc-zh/
So all I have to do is to ask www.boost.org to get answers and go forth. I think the var(switchvalue) is a pointer to a function from the example code in the book:
var(switchvalue) actually returns a function object which returns a reference to switchvalue. A function pointer wouldn't work for numerous reasons. The point I was trying to make was that std::cout << var(switchvalue) doesn't actually do anything. It returns another function object. So, for example, this will work (std::cout << var(switchvalue))(); and will print the value of switchvalue. Not that you'd actually want to write code like that. The correct way is to leave off the var as Zachary noted.
.......... std::for_each(vec.begin(),vec.end(),var(m)=_1); .............. So the operations in m can be performed. Am I right? But I do not know how to save the values in lambda expression.
In Christ, Steven Watanabe
"fmingu"
After your advice,I wrote the following test program: int main(int argc, char *argv[]) { int nnumber=7; int switchvalue=1; if_then_else(var(nnumber)==6, var(switchvalue)=3 ,var(switchvalue)=30); std::cout<<" the nnumber is "<
the value in the lambda expression did not saved. Can you tell me why?
if_then_else only creates and returns a lambda function object.
And How can I correct it?
You need to apply it (note invoking the fn call operator): if_then_else(var(nnumber)==6, var(switchvalue)=3 ,var(switchvalue)=30)(); HTH / Johan
participants (4)
-
fmingu
-
Johan Nilsson
-
Steven Watanabe
-
Zachary Turner