【发布时间】:2020-07-24 09:46:08
【问题描述】:
我正在做一个简单的程序,试图获取地图中不存在的键值对。但是当我运行代码时,我得到一个运行时错误,说==8406==WARNING: AddressSanitizer failed to allocate 0x7ffe7f82c881 bytes
==8406==AddressSanitizer's allocator is terminating the process instead of returning 0
==8406==If you don't like this behavior set allocator_may_return_null=1
#include <iostream>
#include <map>
#include <string>
int main ()
{
typedef std::map<std::string,int> mymap;
mymap cnt;
cnt["a"]=10;
auto f = first.find("asdasd");
std::cout << f->second;
return 0;
}
在我做f->second的那一行。如何处理地图中不存在密钥的情况?
【问题讨论】:
-
“我如何处理地图中不存在密钥的情况” - 简单。只需检查
map::end。