如何返回 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>>?
我有一个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
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中