我试图创建一个幂函数,。 为此,我检查地图上是否有内容,之后可能需要添加或不添加内容。
代码
std::map<char,int> mymap;
std::map<char,int>::iterator it;
mymap['a']=50;
it = mymap.find('a');
int value = it->second;
问题1我怎么知道it
值不null
?我如何获得迭代器的价值?
问题2如何知道it->second
值是不是null
?
问题3如何返回null
的值并将其与std::string/container
进行比较?
例问题3
if(get_client_from_map(id) == nullptr){
// do anything
}else{
// code anything here too
}
注:get_client_from_map(ID)返回(IT->第二)结果,可以的std :: string或空/ nullptr。
'map :: find'返回'map :: end()'如果找不到值,所以你想'if(it!= mymap.end())cout <<“found”; else cout <<“not found”;' – nwp
1.在[std :: map文档](http://www.cplusplus.com/reference/map/map/)中解释2.“int”不能为NULL '3.返回一个std :: string *并检查NULL。 – namezero
现在我明白了,谢谢。 – urb