簡體   English   中英

將函數存儲在數據結構中,以便以后查找和調用它們

[英]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));

這將達到目的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM