C++ 如何将Boost.Phoenix中的一系列语句与std::transform一起使用?

C++ 如何将Boost.Phoenix中的一系列语句与std::transform一起使用?,c++,boost,boost-phoenix,C++,Boost,Boost Phoenix,我想使用Boost.Phoenix创建一个lambda函数,该函数由几行代码组成,然后“返回”一个值,这样我就可以将它与std::transform一起使用 像这样: std::transform(a.begin(), a.end(), b.begin(), ( //Do something complicated here with the elements of a: statement1, statem

我想使用Boost.Phoenix创建一个lambda函数,该函数由几行代码组成,然后“返回”一个值,这样我就可以将它与
std::transform
一起使用

像这样:

std::transform(a.begin(), a.end(), b.begin(),
        (
            //Do something complicated here with the elements of a:
            statement1,
            statement2,
            statement3
            //Is there a way to return a value here?
        )
        );
对于
std::for_each
这将非常有效,但是对于
std::transform
它不会编译,因为逗号运算符返回
void
。如何从类似这样的lambda函数返回值



编辑:我更改了代码片段,因为我首先编写的内容导致了对我想做什么的误解。

不,这是不可能的。从:

与惰性函数和惰性运算符不同,惰性语句总是返回void


(请注意,Boost.Phoenix v3文档中也有相同的断言。)

在函数式编程中,并不打算更改状态。但是,您可以使用
累积
,而不是对每个
使用
。累加意味着您有一个“起始值”(例如,m=1,n=0)和一个向输出值“添加”值的函数:

#include <vector>

struct MN { int m, n; 
  MN(int m,int n):m(m),n(n){}
  static MN accumulate( MN accumulator, int value ) {
     return MN( accumulator.m + value, accumulator.n * value );
  }
}; // your 'state'

int main(){
  std::vector<int> v(10,10); // { 10, 10, ... }
  MN result = std::accumulate( v.begin(), v.end(), MN(0,1), MN::accumulate );
  printf("%d %d", result.m, result.n );
}
#包括
结构MN{int m,n;
MN(int-m,int-n):m(m),n(n){}
静态MN累加(MN累加器,int值){
返回MN(累加器.m+值,累加器.n*值);
}
}; // 你的“状态”
int main(){
向量v(10,10);//{10,10,…}
MN result=std::accumulate(v.begin(),v.end(),MN(0,1),MN::accumulate);
printf(“%d%d”,result.m,result.n);
}

我与Phoenix不相似,但可能有一种方法可以根据它定义
MN::acculate
函数。

也许我应该更清楚地说明,我最初发布的代码片段只是一个示例,与我的实际问题没有多大关系。我的问题涉及复杂的数据结构,
std::transform
无疑是我唯一可以使用的标准算法。我更新了问题以澄清这一点。