映射迭代器,读取字符串字符时出错

map iterator, error reading character of string

我正在尝试在带有前缀的地图中进行部分匹配。

我的 keys 看起来像:"ABCD efg,1234"

我的 values 看起来像:"qqwe,123123,asdad,2000,323232"

认为我传递给密钥匹配的字符串是 "ABCD efg"

  NOTE: the `map` deceleration is elsewhere. It is declered like the following: 
       std::unordered_map<std::string, std::string> umap;

 Code:

std::unordered_map<std::string, std::string>::const_iterator Account::FindPrefix(const std::string& search_for)
{
    std::unordered_map<std::string, std::string>::const_iterator got = umap.lower_bound(search_for);

    if (got != umap.end())
    {
        const std::string& key = got->first;
        if (key.compare(0, search_for.size(), search_for) == 0)
            return got;
    }
    return umap.end();
}

代码已编译,但总是返回 umap.end(),并且从未返回 got,所以我使用了调试器,我注意到 constant iterator got 的值是 (<Error reading characters of string>,<Error reading characters of string>)

注意:我确实检查过以确保我将值正确地提供给 map,似乎没问题,因为我可以看到它已填充。

注意2:Kabanus建议在if语句之前输出got。当它达到 std::cout<<got->first; 程序崩溃时,我得到以下消息:

Exception thrown at 0x57EF65F6 (msvcp140d.dll) in BankManagment.exe: 0xC0000005: Access violation reading location 0xCDCDCDCD.

编辑: 我不知道为什么编译器没有捕获 lower_bound 与无序映射的使用

编辑: 以下是 decltype 的错误消息(在 post 的评论中请求)

您想使用find方法:http://www.cplusplus.com/reference/unordered_map/unordered_map/find/

如果是 std::map,则下限会标记项目本身(如果已经存在)或可能添加项目的位置 ("insert hint")。这有利于高效的查找或插入操作(您不需要再次遍历树)。你只是在模仿 find 已经做了什么,在这里(也就是说,如果它是 std::map,再次)...