c++ unorderedmap vector subscript out of range

ⅰ亾dé卋堺 提交于 2019-12-11 15:07:24

问题


Currently when I have been calling my getFunction method I am getting nasty crash. My program compiles fine, but when I run and call this function, I get "Debug Assertion Failed!", "Expression: vector subscript out of range". Not sure how to deal with this as I haven't been doing much in c++ for a few years.

void* PluginMap::getFunction(char* pguid, char* fname){
    if(plugin_map.size()>0 && plugin_map.find(pguid)!=plugin_map.end())
    { 
        //plugin_map is an unorderedmap that is defined elsewhere.
        MicroMap* mm = &plugin_map[pguid];
        if((*mm).find(fname)!=(*mm).end())
        {
            //MicroMap is an unorderedmap that goes in plugin_map, and contains void*
            return (*mm)[fname];
        }
    }
    return 0;
}

Any help would be appreciated.


回答1:


Please avoid char* with std::unordered_map use proper std::string and it should be fine. char* is taken as pointer type and which might cause an issue unless you have defined std::hash for it.



来源:https://stackoverflow.com/questions/15445397/c-unorderedmap-vector-subscript-out-of-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!