久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用函數指針的 STL 映射

Using a STL map of function pointers(使用函數指針的 STL 映射)
本文介紹了使用函數指針的 STL 映射的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我開發了一個具有許多內置函數的腳本引擎,所以要調用任何函數,我的代碼只是進入了一個 if .. else if .. else if 墻檢查名稱,但我想開發一個更有效的解決方案.

I developed a scripting engine that has many built-in functions, so to call any function, my code just went into an if .. else if .. else if wall checking the name but I would like to develop a more efficient solution.

我應該使用 hashmap 以字符串作為鍵和指針作為值嗎?我怎么能通過使用 STL 映射來做到這一點?

Should I use a hashmap with strings as keys and pointers as values? How could I do it by using an STL map?

編輯:我想到的另一點是:當然使用映射會迫使編譯器不內聯函數,但我的低效方法沒有因函數調用的必要性而產生的任何開銷,它只是執行代碼.

EDIT: Another point that came into my mind: of course using a map will force the compiler not to inline functions, but my inefficient approach didn't have any overhead generated by the necessity of function calls, it just executes code.

所以我想知道函數調用產生的開銷是否會比使用 if..else 鏈更好..否則我可以通過在運行時檢查一個字符來最小化比較次數(會更長但更快).

So I wonder if the overhead generated by the function call will be any better than having an if..else chain.. otherwise I could minimize the number of comparisons by checking a character at runtime (will be longer but faster).

推薦答案

無論你的函數簽名是什么:

Whatever your function signatures are:

typedef void (*ScriptFunction)(void); // function pointer type
typedef std::unordered_map<std::string, ScriptFunction> script_map;

// ...

void some_function()
{
}

// ...

script_map m;
m.emplace("blah", &some_function);

// ...

void call_script(const std::string& pFunction)
{
    auto iter = m.find(pFunction);
    if (iter == m.end())
    {
        // not found
    }

    (*iter->second)();
}

注意 ScriptFunction 類型可以泛化為 std::function</*whatever*/> 這樣你就可以支持任何可調用的東西,而不僅僅是函數指針.

Note that the ScriptFunction type could be generalized to std::function</* whatever*/> so you can support any callable thing, not just exactly function pointers.

這篇關于使用函數指針的 STL 映射的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數)
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 亚洲影院在线 | 国产精品看片 | a在线视频| 国产色站 | a一级黄色片 | 日韩欧美国产精品 | 成人福利视频在线观看 | 欧美日韩毛片 | 久久精品国产精品 | 一区二区欧美日韩 | 日韩不卡一区二区 | 夜夜肉她怀孕h周君彦 | 免费网站观看www在线观看 | 91亚洲国产成人精品性色 | 欧美在线免费 | 亚洲乱码在线 | 日韩高清精品免费观看 | av网在线观看 | 欧美激情综合网 | 久久久久久毛片 | 精品久久久久久久久久 | 麻豆chinese新婚xxx | 亚洲综合色网 | 久在线视频| 国产一级黄色大片 | 黄色av一区 | 久久久免费精品视频 | 欧美精品在线看 | 韩日在线视频 | 91视频在线看 | 黄色片观看 | 一级片av| 久久黄色| 精品一区二区三区四区五区 | 国产黄色免费视频 | 久久久久久综合 | 日韩网站在线观看 | 免费成人小视频 | 久久久久久黄色 | 亚洲精品在 | 国产又粗又黄 |