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

使用函數(shù)指針的 STL 映射

Using a STL map of function pointers(使用函數(shù)指針的 STL 映射)
本文介紹了使用函數(shù)指針的 STL 映射的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我開(kāi)發(fā)了一個(gè)具有許多內(nèi)置函數(shù)的腳本引擎,所以要調(diào)用任何函數(shù),我的代碼只是進(jìn)入了一個(gè) if .. else if .. else if 墻檢查名稱(chēng),但我想開(kāi)發(fā)一個(gè)更有效的解決方案.

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.

我應(yīng)該使用 hashmap 以字符串作為鍵和指針作為值嗎?我怎么能通過(guò)使用 STL 映射來(lái)做到這一點(diǎn)?

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

編輯:我想到的另一點(diǎn)是:當(dāng)然使用映射會(huì)迫使編譯器不內(nèi)聯(lián)函數(shù),但我的低效方法沒(méi)有因函數(shù)調(diào)用的必要性而產(chǎn)生的任何開(kāi)銷(xiāo),它只是執(zhí)行代碼.

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.

所以我想知道函數(shù)調(diào)用產(chǎn)生的開(kāi)銷(xiāo)是否會(huì)比使用 if..else 鏈更好..否則我可以通過(guò)在運(yùn)行時(shí)檢查一個(gè)字符來(lái)最小化比較次數(shù)(會(huì)更長(zhǎng)但更快).

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).

推薦答案

無(wú)論你的函數(shù)簽名是什么:

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 類(lèi)型可以泛化為 std::function</*whatever*/> 這樣你就可以支持任何可調(diào)用的東西,而不僅僅是函數(shù)指針.

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

這篇關(guān)于使用函數(shù)指針的 STL 映射的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

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++ 中旋轉(zhuǎn)圖像而不使用 OpenCV 函數(shù))
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開(kāi) avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無(wú)法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 青青草网 | 中文字幕在线观看成人 | 激情在线视频 | 在线国产视频 | 亚洲不卡在线观看 | 国产精品美女www | 一本一道久久a久久精品蜜桃 | 羞羞视频网站免费观看 | 久久亚洲一区二区 | 亚洲一区视频在线 | 大乳boobs巨大吃奶挤奶 | 国产乱码久久久 | 亚洲一区在线日韩在线深爱 | 成人久久久 | 日韩av一区二区在线观看 | 超碰精品在线观看 | 国产成人综合亚洲欧美94在线 | 成人在线网址 | 成人毛片一区二区三区 | 欧美色欧美亚洲另类七区 | 免费一级欧美在线观看视频 | 综合久久色 | 97国产精品视频 | 国产精品久久国产精品 | 欧美日韩一卡二卡 | 亚洲成av人影片在线观看 | 国产欧美日韩精品一区二区三区 | 高清国产一区二区 | 一区二区三区亚洲 | 毛片在线免费播放 | 国产精品久久久久一区二区三区 | 精品视频在线免费观看 | 国产精品久久av | 久久黄色网 | 久久国产精品一区二区 | 日韩一区在线播放 | 精品国产一区二区三区性色 | 久久久精品一区二区三区四季av | 岛国av一区二区三区 | 精品国产高清一区二区三区 | 欧美性大战久久久久久久蜜臀 |