c++ unorderedmap vector subscript out of range

2019-08-29 05:56发布

问题:

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.