C++ map注意事项

std::map<int, std::string> map;

判断key是否存在时不能使用:std::string str = map[9];   //这样不存在时会新增!!!

需要这样判断:

std::map<int, std::string>::interator iter;

iter = map.find(9);

if(iter!=map.end())   //存在

else //不存在

posted @ 2023-04-09 11:09  远方是什么样子  阅读(10)  评论(0编辑  收藏  举报