/*
Lambda 表达式中的控制结构
参考网址:
//http://club.topsage.com/thread-2276569-1-1.html
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#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<std::string> 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)<=3u,
cout << constant("less than 3 \n")));
std::for_each(vec.begin(),vec.end(),
if_(bind(&std::string::size,_1)<=3u)
[ _1 += "MM",cout << _1 << '\n' ] );
//if_then_else -----------------------------------------
std::for_each(vec.begin(),vec.end(),
if_then_else( bind(&std::string::size,_1)<=3u,
std::cout << _1 << '\n',
std::cout << constant("Skip.\n")));
std::for_each(vec.begin(),vec.end(),
if_(_1=="4444")
[_1 += "DD",cout << _1 << '\n']
.else_[cout << _1 << '\n' ] );
//if_then_else_return -----------------------------------
//只有这一种方式
std::transform(vec.begin(),vec.end(),vec.begin(),
if_then_else_return(bind(&std::string::size,_1)<=3u,
_1 + "m",
_1 + "g"
) );
//switch_ ------------------------------------------------
//<0>它们必须是整型的常量表达式,最多九个,且隐式地增加了一个break-语句
//每个case 只能执行一条语句
std::for_each(vec.begin(),vec.end(),
switch_statement
(
bind(&std::string::size,_1),
case_statement<3> (var(cout) << "3位数:"<< _1 << '\n' ),
case_statement<2> ( cout << constant("2位数") << '\n' ),
default_statement ( cout << "多位数"+ _1 << '\n' )
)
);
//while_loop ---------------------------------
int val1=1;
int val2=4;
(while_loop(_1<_2,
(++_1,_1*2/2,std::cout << constant( "Inc...\n")))
)(val1,val2);
val1=1;
val2=4;
(while_(_1<_2)
[++_1,std::cout << constant("Inc...\n")]
)(val1,val2);
//do_while_loop -------------------------------
//make_const,它只是返回一个对它的参数的 const 引用。另一个方法是把整个 lambda 表达式用于调用 const_parameters
(do_while_loop(
_1!=12,
cout << constant("I'll run once"))
)(make_const(12));
(do_[ cout << constant("I'll run once")]
.while_(_1!=12)
)(make_const(12));
//for_loop ---------------------------------
//命名的延期变量用于避免重复为常量和变量敲入 constant 或 var 。它们对你要用的东西进行命名并稍后可被引用。
int val3=0;
var_type<int>::type counter(var(val3));
(for_loop(counter=0,counter<_1,++counter,
var(std::cout) << "counter is " << counter << "\n")
)(make_const(4));
(for_(counter=0,counter<_1,++counter)
[counter+1,var(cout) << "counter is " << counter << "\n"]
)(make_const(4));
}
Lambda 表达式中的控制结构
参考网址:
//http://club.topsage.com/thread-2276569-1-1.html
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#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<std::string> 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)<=3u,
cout << constant("less than 3 \n")));
std::for_each(vec.begin(),vec.end(),
if_(bind(&std::string::size,_1)<=3u)
[ _1 += "MM",cout << _1 << '\n' ] );
//if_then_else -----------------------------------------
std::for_each(vec.begin(),vec.end(),
if_then_else( bind(&std::string::size,_1)<=3u,
std::cout << _1 << '\n',
std::cout << constant("Skip.\n")));
std::for_each(vec.begin(),vec.end(),
if_(_1=="4444")
[_1 += "DD",cout << _1 << '\n']
.else_[cout << _1 << '\n' ] );
//if_then_else_return -----------------------------------
//只有这一种方式
std::transform(vec.begin(),vec.end(),vec.begin(),
if_then_else_return(bind(&std::string::size,_1)<=3u,
_1 + "m",
_1 + "g"
) );
//switch_ ------------------------------------------------
//<0>它们必须是整型的常量表达式,最多九个,且隐式地增加了一个break-语句
//每个case 只能执行一条语句
std::for_each(vec.begin(),vec.end(),
switch_statement
(
bind(&std::string::size,_1),
case_statement<3> (var(cout) << "3位数:"<< _1 << '\n' ),
case_statement<2> ( cout << constant("2位数") << '\n' ),
default_statement ( cout << "多位数"+ _1 << '\n' )
)
);
//while_loop ---------------------------------
int val1=1;
int val2=4;
(while_loop(_1<_2,
(++_1,_1*2/2,std::cout << constant( "Inc...\n")))
)(val1,val2);
val1=1;
val2=4;
(while_(_1<_2)
[++_1,std::cout << constant("Inc...\n")]
)(val1,val2);
//do_while_loop -------------------------------
//make_const,它只是返回一个对它的参数的 const 引用。另一个方法是把整个 lambda 表达式用于调用 const_parameters
(do_while_loop(
_1!=12,
cout << constant("I'll run once"))
)(make_const(12));
(do_[ cout << constant("I'll run once")]
.while_(_1!=12)
)(make_const(12));
//for_loop ---------------------------------
//命名的延期变量用于避免重复为常量和变量敲入 constant 或 var 。它们对你要用的东西进行命名并稍后可被引用。
int val3=0;
var_type<int>::type counter(var(val3));
(for_loop(counter=0,counter<_1,++counter,
var(std::cout) << "counter is " << counter << "\n")
)(make_const(4));
(for_(counter=0,counter<_1,++counter)
[counter+1,var(cout) << "counter is " << counter << "\n"]
)(make_const(4));
}