如何在 unordered_map 中使用 cpp 来识别 lambda 函数?
How to asaign lambda funcion in unordered_map with cpp?
目前正在尝试学习 c++,我正在尝试制作一个"狩猎 wumpus"应用程序来帮助我学习。我在将 lambda 函数分配给 C++ 中的unordered_map时遇到问题。我的 IDE 给我错误 "参数类型不匹配:不兼容的指针类型'节点*const'和'节点(((节点*('">
#include <iostream>
#include <string>
#include <unordered_map>
class node{
public:
int title;
node *adj[8];
std::string desc;
}
...
bool shoot(node *player){
std::unordered_map<std::string, node*> adjLambda; //lambda to return *left, *up, etc
for (int i; i < 8; i++){
if (player->adj[i]->title != player->title){ //empty adjacencies points towards self
adjLambda.insert(std::pair <std::string, node*> (std::to_string(player->adj[i]->title), [](node *n) { return n->adj[i];}));
}
}
}
您可以包含<functional>
,然后将值存储为std::function
:
void shoot(node *player){
std::unordered_map<std::string, std::function<node*(node*)>> adjLambda; //lambda to return *left, *up, etc
for(int i{}; i < 8; i++){
if(player->adj[i]->title != player->title){ //empty adjacencies points towards self
adjLambda.insert( // insert into the unordered map
std::pair< // pair to be inserted
std::string, // key is a string
std::function< // map entry is a function
node*(node*) // function takes a node pointer and returns a node pointer
>
>(
std::to_string(player->adj[i]->title), // create a string to use as the key
[i](node *n) { return n->adj[i]; } // lambda that takes a node pointer and returns another
)
);
}
}
}
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- Linux的Cpp上的计时器
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 命名空间中具有.h和.cpp文件的类
- 内置函数可查看CPP中的成员变量
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- Haskell堆栈设置 - 无法识别CPP程序
- 如何在 unordered_map 中使用 cpp 来识别 lambda 函数?
- cpp 预处理器无法识别 UTF-32 字符串
- 谷歌语音识别(cpp示例):对AssignDescriptors()的未定义引用
- 在cpp项目(VS2005)内部编译的c模块中无法识别内联关键字
- 无法识别第二个cpp文件中的任何错误
- c 如何从类类 .h 文件内部识别.cpp中的 UDT 枚举
- 将.cpp更改为 .cu 后,>> C++ 代码中无法识别基本操作数
- 来自单独.cpp文件中同一命名空间的 free 函数,C++ 中无法识别
- jenkins在构建cpp代码时不会识别错误
- singleton--cpp文件中无法识别的静态成员
- CPP:Eclipse无法识别"长长"类型
- Arduino IDE不识别.c文件是.cpp