如何返回 boost::fusion::vector<x,y,z> 元素以添加到 std::array<boost::fusion::vector<x,y,z>>?

How to return boost::fusion::vector<x,y,z> elements to add to a std::array<boost::fusion::vector<x,y,z>>?

本文关键字:lt gt vector boost fusion 添加 array std 返回 何返回 元素      更新时间:2023-10-16

我有一个std::array和一个boost::fusion::vector<X, Y>,我想将它们传递给func1()。此函数将向每个std::array元素添加一个boost::fusion::vector<X, Y>实例。

我必须使用fusion::fold(),这样我才能将正确数量的元素添加到fusion::vector<X,Y>中,对吗

所以我现在有这样的东西:

void func1(){
boost::fusion::vector<X,Y> my_vec;
std::array<boost::fusion::vector<X,Y> > my_array[10];
func2(my_vec, my_array);
}
void func2(boost::fusion::vector<X,Y> my_vec, std::array<boost::fusion::vector<X,Y> > my_array){
//THIS IS THE PART I AM UNSURE ABOUT
for(int k=0; k<10; k++){
//The first two parameters aren't so important- just included to show the idea
my_array[k] = boost::fusion::fold(my_vec, 1, some_struct);
}
}
//This part is irrelevant
struct some_struct
{
typedef int result_type;
template<typename T>
int operator()(int x, T& t) const
{
t = something(x);
//Not sure if this part needs to return a boost::fusion::vector<X, Y>
return x;
}
};

我不确定的部分是如何使用my_vec的签名来创建多个boost::fusion::vector<X,Y>实例并将其返回,以便在func2()中添加到数组中。

有人能提供建议吗?

编辑-刚刚发现我把fold()的第一个参数弄错了,已经修改了我的问题。

我不确定我是否理解你的问题,所以首先让我们解释一下要澄清的内容。

通常(不仅仅是为了融合),"折叠"一个具有两个参数的函数是将其应用于向量的每个元素,以及将函数应用于向量前一个元素的结果。第一个元素被赋予初始值。

因此,如果我将要折叠的函数定义为A f(A, B),则该函数的折叠将等效于(对于4个元素):

f(f(f(f(A_initial_value, B_0), B_1), B_2), B_3);

(大写前缀只是为了强制类型)

现在,更准确地说是融合褶皱。它将折叠boost::fusion::vector<>内部所有元素的功能。由于boost::fusion::vector<X, Y>相当于std::tuple<X,Y>,它将在不同类型上调用f

A_final_value = f(A_initial_value, X_value), Y_value);

所以,当你这样做时:

my_array[k] = boost::fusion::fold(my_vec, 1, some_struct);

my_array[k]将收到一个数值,由于您已将其定义为fusion::vector<X,Y>,因此该数值不会编译

因此,在试图澄清fold之后,我会对您的问题说"不",但我承认我不理解您所说的"向fusion::vector<X,Y>添加正确数量的元素"是什么意思。

编辑:根据评论中的内容进行更新。目标是在std::array<fusion::vector<int, int, int, int>>中生成一个斐波那契序列,例如遍历array的每个vector将以正确的顺序给出斐波那奇。

使用fusion,您必须在遍历vector的元素时传递状态,因此fold是一个不错的选择。

以下是我的建议(但从第二个元素而不是第一个元素开始斐波那契序列,因为我没有费心处理特殊情况…对不起:):

template <typename Value>
struct Generator
{
// Easier to read and mandatory for fold
typedef typename std::tuple<Value, Value> result_type;
// The function that generate the fibonacci and update the Value  
result_type operator()(result_type previous, Value& elem) const
{
elem = std::get<0>(previous) + std::get<1>(previous);
return std::make_tuple(std::get<1>(previous), elem);
}
};
// Use template to be a bit more generic on array size and vector type
template <typename Vector, size_t array_size, typename Value>
void func2(std::array<Vector, array_size>& array, std::tuple<Value, Value> init)
{
// The state that will be fed across the fold function
auto previous = init;
for (auto& vect: array)
{        
// Generate the fibonnaci value for every element of the vector starting
// from where the state is. The return value of fold is the new state
previous = boost::fusion::fold(vect, previous, Generator<Value>());
}
}
// Tool to print the vector
struct Printer
{
template <typename Elem>
void operator()(const Elem& elem) const
{
std::cout << elem << std::endl;
}
};
// Use template to be a bit more generic on array size and vector type
template <typename Vector, size_t array_size, typename Value>
void func1(std::tuple<Value, Value> init)
{    
// Create the vector
std::array<Vector, array_size> array;
// FIll it with fibonacci
func2(array, init);
// Print it
for (auto vect: array)
{        
boost::fusion::for_each(vect, Printer());
}   
}

http://rextester.com/XCXYX58360