在使用std:map時需要判斷某個key是否存在map中,
需要使用方法find,操作符[] 不能使用
如下
std:map<int,int> test
//判斷map中是否存在key為200的健值對。
if(test[200] == null) {// 這種方法有風險,原因是這個操作如果key在map中不存在,則為200在map中創建一個默認值。
//TODO
}
if(test.find(200) != m_heroMap.end()) {// 正確方法
//TODO
}
方法的註釋說明
* Allows for easy lookup with the subscript ( @c [] )
* operator. Returns data associated with the key specified in
* subscript. If the key does not exist, a pair with that key
* is created using default values, which is then returned.