stl map的使用

map[a]=b,如果键值a不存在,则会创建新元素。所以只有当构造map时使用[],而在访问map时为安全起见,不要用[],而使用find:

Cobj*getObjByName(const map<string,Cobj*>&objMap,const string&name){

  std::map<string,Cobj*>::iterator it;

    it=objMap.find(name);

    if(it==objMap.end()){

        return NULL;

    }else{

        return it->second;

    }

 }

参考:

http://www.cplusplus.com/reference/map/map/operator[]/

http://www.cplusplus.com/reference/map/map/find/

补充:遍历map: http://blog.sina.com.cn/s/blog_42af9c0201000b8b.html

原文地址:https://www.cnblogs.com/wantnon/p/4425404.html