【问题标题】:c++ unorderedmap vector subscript out of rangec++ unordered_map向量下标超出范围
【发布时间】:2013-03-16 03:57:37
【问题描述】:

目前,当我一直在调用我的 getFunction 方法时,我遇到了严重的崩溃。我的程序编译得很好,但是当我运行并调用这个函数时,我得到“调试断言失败!”,“表达式:向量下标超出范围”。不知道如何处理这个问题,因为几年来我没有在 C++ 中做太多事情。

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;
}

任何帮助将不胜感激。

【问题讨论】:

  • 你在使用迭代器吗?因为如果你是,我在你的代码中看不到任何东西。
  • 不,我没有使用任何迭代器。
  • 如果您使用 std::unordered_map,迭代器会使它大大更容易。这是一个例子:cplusplus.com/reference/unordered_map/unordered_map/find

标签: c++ vector range runtime-error unordered-map


【解决方案1】:

请避免 char* 与 std::unordered_map 使用正确的 std::string 应该没问题。 char* 被视为指针类型,除非您为它定义了 std::hash,否则它可能会导致问题。

【讨论】:

  • 谢谢,c++ 对我来说有点困惑,因为我一直在 js 中工作,而 c#,堆栈社区很棒!
猜你喜欢
  • 2013-04-04
  • 2021-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 2015-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多