/// 一个hash_map死循环的例子: class obj { public: obj(char *_name) { strncpy(name, _name, 31); } char name[32]; /// anyothers }; hash_map<char*, obj*> list; typedef hash_map<char*, obj*>::iterator hash_iter; obj *pObj = new obj("testUnion"); if(pObj) { list.insert(pObj->name, pObj); } strncpy(pObj->name, "hahaha", 31); /// 这里就会死循环 for(hash_iter it = list.begin(); it != list.end(); ++it) { /// ... } /// 原因,见__gnu_cxx::hash_map的源代码,一句一句分析 /// for循环执行第一句hash_iter it = list.begin(); 这里调用了函数hash_map::begin(): /// __gnu_cxx::hash_map中begin()定义如下 iterator begin() { return _M_ht.begin(); }; /// 我们看下_M_ht是什么东东,__gnu_cxx::hash_map有一个typedef typedef hashtable<pair<const _Key, _Tp>, _Key, _HashFcn, _Select1st<pair<const _Key, _Tp> >, _EqualKey, _Alloc> _Ht; _Ht _M_ht; /// 也就是说_M_ht是一个hashtable类型