[英]How can I access std::function in std::list in C++
I am attempting to answer generate a Template function in C++ which takes in an std::list of std::function (I think). 我试图回答在C ++中生成一个Template函数,该函数接受std :: function的std :: list(我认为)。 I am however not sure how to understand the datatype I am working with.
但是,我不确定如何理解正在使用的数据类型。
According to GDB my datatype is: 根据GDB,我的数据类型是:
type = std::__cxx11::list<std::function<void(std::array<positions, 3>&)>, std::allocator<std::function<void(std::array<positions, 3>&)> > > (*)(const std::array<positions, 3> &)
I can not access the element as an array if I call the input movenents
I can not access for example the first element as movement[0]
I don't understand why that is since the type looks like a list. 如果调用输入的
movenents
则无法访问该元素作为数组;例如,无法访问的第一个元素,其movement[0]
;由于类型看起来像列表,所以我不明白为什么。
I have tried to access it as an array, and I have tried to read std::list containing std::function and access it with: 我试图以数组形式访问它,并且试图读取包含std :: function的std :: list并使用以下命令进行访问:
for (auto f: movements) {
(*f)();
}
The function generating the the list looks like this: 生成列表的函数如下所示:
auto movements(const pieces_positions &pieces) {
auto result = std::list<std::function<void(pieces_positions&)>>{};
for (auto i=0u; i<pieces.size(); ++i)
switch(pieces[i]) {
case positions::pos1:
result.push_back([i](pieces_positions& pieces){ pieces[i] = positions::pos2; });
break;
case positions::pos2:
result.push_back([i](pieces_positions& pieces){ pieces[i] = positions::pos1; });
result.push_back([i](pieces_positions& pieces){ pieces[i] = positions::pos3; });
break;
case positions::pos3:
result.push_back([i](pieces_positions& pieces){ pieces[i] = positions::pos2; });
break;
}
return result;
}
Two things (that I think are what you're asking about): 两件事(我想这是您要问的问题):
The symbol movements
is a function that you need to call. 符号
movements
是您需要调用的功能 。 You do it by the usual movements(pices)
. 您通过通常的
movements(pices)
完成它。
The function objects in the list that the function returns are not pointers that can be dereferenced. 函数返回的列表中的函数对象不是可以取消引用的指针。 You use them as normal functions and call them as such, like
f(pieces)
. 您可以将它们用作常规函数,并像
f(pieces)
这样调用它们。
Also, in C++ there's no standard "array-list" like container. 另外,在C ++中,没有像容器这样的标准“数组列表”。 A list is a list and can't be indexed like an array or a vector.
列表是列表,不能像数组或向量一样被索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.