将函数存储在数据结构中以便以后查找和调用它们

Storing functions in data structure to later find and call them

我正在考虑创建一个程序来将函数存储在未排序的映射中。是否可以通过他们的钥匙找到他们并打电话给他们?我还想将其用作其他模块中的头文件。它会寻址相同的内存地址位置还是每次都会生成新的内存?

你是说

std::unordered_map<some_key_type, std::function<Output(Input)>> functions;
// ...
functions[some_value](some_input);

?

如果你这样做,请参阅相关文档:

#include <functional>
using namespace std;

int add(int a, int b)
{
       return a+b;
}
int main()
{
       unordered_map<int , function<int(int,int) >> umap ;
       //umap[1](5,10);

}
**
 output :
  std : bad function call

enter code here
**
I want to map that function attibute in value section to int add function 
I am unable to map it or how I can write the 'values' function body

您还没有为 umap 的键 1 分配任何功能。

umap.insert(make_pair(1, add));

这样就可以了。