---
title: "Lambda 表达式中的控制结构"
author: "lunan"
date: "2011-11-06T15:35:50Z"
tags: ["Lambda"]
categories: ["编程语言"]
slug: "lambda"
draft: false
lastmod: "2011-11-06T15:35:50Z"
id: 1236728
url: "https://lunan.iteye.com/blog/1236728"
---

/* Lambda 表达式中的控制结构 参考网址： //http://club.topsage.com/thread-2276569-1-1.html \*/ #include #include #include #include #include "boost/lambda/lambda.hpp" #include "boost/lambda/bind.hpp" #include "boost/lambda/if.hpp" //if #include "boost/lambda/switch.hpp" //switch #include "boost/lambda/loops.hpp" //while,do,for int main() { using namespace boost::lambda; using namespace std; std::vector vec; vec.push\_back("1"); vec.push\_back("22"); vec.push\_back("333"); vec.push\_back("4444"); //if\_then ---------------------------------------------- //首先，条件是一个谓词，它必须是一个 lambda 表达式,其次，then-语句必须也是一个 lambda 表达式,第三，else-语句必须也是一个 lambda 表达式 //用 constant 来把字符串变成一个 lambda 表达式。 //6u, 这是为了确保执行的是两个无符号类型的比较。 //两种结构,后一种可以多个“,”多条语句 std::for\_each(vec.begin(),vec.end(), if\_then( bind(&std::string::size,\_1)&lt;=3u, cout &lt;&lt; constant("less than 3 \\n"))); std::for\_each(vec.begin(),vec.end(), if\_(bind(&std::string::size,\_1)&lt;=3u) \[ \_1 += "MM",cout &lt;&lt; \_1 &lt;&lt; '\\n' ] ); //if\_then\_else ----------------------------------------- std::for\_each(vec.begin(),vec.end(), if\_then\_else( bind(&std::string::size,\_1)&lt;=3u, std::cout &lt;&lt; \_1 &lt;&lt; '\\n', std::cout &lt;&lt; constant("Skip.\\n"))); std::for\_each(vec.begin(),vec.end(), if\_(\_1=="4444") \[\_1 += "DD",cout &lt;&lt; \_1 &lt;&lt; '\\n'] .else\_\[cout &lt;&lt; \_1 &lt;&lt; '\\n' ] ); //if\_then\_else\_return ----------------------------------- //只有这一种方式 std::transform(vec.begin(),vec.end(),vec.begin(), if\_then\_else\_return(bind(&std::string::size,\_1)&lt;=3u, \_1 + "m", \_1 + "g" ) ); //switch_ ------------------------------------------------ //&lt;0&gt;它们必须是整型的常量表达式,最多九个，且隐式地增加了一个break-语句 //每个case 只能执行一条语句 std::for\_each(vec.begin(),vec.end(), switch\_statement ( bind(&std::string::size,\_1), case\_statement&lt;3&gt; (var(cout) &lt;&lt; "3位数:"&lt;&lt; \_1 &lt;&lt; '\\n' ), case\_statement&lt;2&gt; ( cout &lt;&lt; constant("2位数") &lt;&lt; '\\n' ), default\_statement ( cout &lt;&lt; "多位数"+ \_1 &lt;&lt; '\\n' ) ) ); //while\_loop --------------------------------- int val1=1; int val2=4; (while\_loop(\_1&lt;\_2, (++\_1,\_1\*2/2,std::cout &lt;&lt; constant( "Inc...\\n"))) )(val1,val2); val1=1; val2=4; (while\_(\_1&lt;\_2) \[++\_1,std::cout &lt;&lt; constant("Inc...\\n")] )(val1,val2); //do\_while\_loop ------------------------------- //make\_const，它只是返回一个对它的参数的 const 引用。另一个方法是把整个 lambda 表达式用于调用 const\_parameters (do\_while\_loop( \_1!=12, cout &lt;&lt; constant("I'll run once")) )(make\_const(12)); (do\_\[ cout &lt;&lt; constant("I'll run once")] .while\_(\_1!=12) )(make\_const(12)); //for\_loop --------------------------------- //命名的延期变量用于避免重复为常量和变量敲入 constant 或 var 。它们对你要用的东西进行命名并稍后可被引用。 int val3=0; var\_type::type counter(var(val3)); (for\_loop(counter=0,counter&lt;\_1,++counter, var(std::cout) &lt;&lt; "counter is " &lt;&lt; counter &lt;&lt; "\\n") )(make\_const(4)); (for\_(counter=0,counter&lt;\_1,++counter) \[counter+1,var(cout) &lt;&lt; "counter is " &lt;&lt; counter &lt;&lt; "\\n"] )(make\_const(4)); }