久久久久久久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 轉換單一顏色)
主站蜘蛛池模板: 久久99国产精品久久99果冻传媒 | 99久久免费观看 | 偷拍自拍在线观看 | 日韩在线免费视频 | 国产成人综合亚洲欧美94在线 | 国产精品178页 | 国产激情福利 | 99视频免费看 | 国产成人精品一区 | 久草视频在线播放 | 亚洲欧美中文日韩在线 | 久草在线影 | 99色播 | 99成人在线视频 | 91成人在线 | 精品国产免费人成在线观看 | 国产精品18hdxxxⅹ在线 | 日本一区精品 | 亚洲国产成人久久久 | 91精品国产91久久久久游泳池 | 麻豆久久久9性大片 | 伊人精品在线视频 | 欧美极品少妇xxxxⅹ免费视频 | 在线视频日韩 | 国产一区二区三区免费观看在线 | 国产精品不卡 | 成人欧美一区二区三区在线播放 | 亚洲成人天堂 | 国产成人一区二区 | 在线观看亚洲 | 99精品国产一区二区三区 | 久久久久亚洲 | 国产精品无码久久久久 | 久热久热 | 超碰精品在线观看 | 国产福利在线看 | 一本一道久久a久久精品蜜桃 | 成人a视频片观看免费 | 成人h视频在线观看 | 91成人精品 | 亚洲精品在线免费 |