gpt4 book ai didi

c++ - 如何返回 boost::fusion::vector 元素以添加到 std::array>?

转载 作者:行者123 更新时间:2023-11-30 02:50:05 25 4
gpt4 key购买 nike

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

我必须使用 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() 的第一个参数错了,修改了我的问题。

最佳答案

我不确定我是否真的理解你的问题所以首先让我们解释一下 fold是试图澄清。

通常(不仅仅是为了 fusion )“fold ”一个带有两个参数的函数是将它应用于 vector 的每个元素以及将该函数应用于 vector 的前一个元素的结果。第一个元素被赋予初始值。

因此,如果我将要 fold 的函数定义为 A f(A, B) ,这个函数的 fold 将等同于(对于 4 个元素):

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

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

现在,更精确地 fusion fold .它将 fold 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>> 中生成斐波那契数列比如走路各vectorarray将以正确的顺序给出斐波那契数列。

使用 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

关于c++ - 如何返回 boost::fusion::vector<x,y,z> 元素以添加到 std::array<boost::fusion::vector<x,y,z>>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20723102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com