小编Les*_*lie的帖子

为unordered_map赋值的基本问题

我有一个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++

10
推荐指数
1
解决办法
4591
查看次数

标签 统计

c++ ×1