简体   繁体   English

如何在C ++中的std :: list中访问std :: function

[英]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): 两件事(我想这是您要问的问题):

  1. The symbol movements is a function that you need to call. 符号movements是您需要调用的功能 You do it by the usual movements(pices) . 您通过通常的movements(pices)完成它。

  2. 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.

相关问题 如何编写基于处理返回不同类型的std :: list的C ++函数? - How can I write a C++ function that returns different types of std::list based on processing? 我可以通过整数索引访问c ++ std :: map中的元素吗? - Can I access the elements in a c++ std::map by an integer index? 如何在C ++中使用std :: function访问Functor的成员函数? - How to access the member functions of a Functor using std::function in C++? 如何在不解除分配的情况下将元素与 c++ std::list 断开链接? - How can I delink an element from a c++ std::list without deallocating it? 如何在C ++中为运算符分配std :: function? - How do I assign an std::function to an operator in C++? 在C ++中如何使用模板函数作为std :: for_each中的第3个参数? - In C++ how can I use a template function as the 3rd parameter in std::for_each? 如何通过通用引用或 std::forward 将这三个 c++ 模板 function 合并为一个? - How can I merge this three c++ template function into one by universal reference or std::forward? 如何唯一标识 C++ 中的 std::function 中包含的 lambda(可能正在捕获) - How can I uniquely identify a lambda (may be capturing) contained within a std::function in C++ 如何解决 gcc 中的 c++ std::rintf 问题 - How can I solve c++ std::rintf issue in gcc 如何在C ++中随机播放std :: stack? - How can I shuffle a std::stack in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM