我有一个unordered_map,它将int作为键存储为指针作为值.我需要检查密钥的存在.如果密钥不可用,我需要插入密钥和值.哪一个更好的方法?
谢谢.
unordered_map<int, classA*>testMap;
classA* ptr = testMap[1];
if(ptr == NULL)
testMap[1] = new classA;
OR
unordered_map<int, classA*>::iterator it = testMap.find(1);
if(it == testMap.end())
{
testMap.insert(make_pair(1, new classA));
}
Run Code Online (Sandbox Code Playgroud) c++ ×1