// Q1. only one iterm was inserted. what's wrong??Code:#include <iostream> #include <ext/hash_map> #include <string> #include <map> #include <stdio.h> #include <string.h> using namespace std; class subInfo{ public: float Weight; subInfo(){} subInfo(float _w){ Weight = _w; } ~subInfo(){} bool operator<(const subInfo & rsh){ return Weight < rsh.Weight; } }; typedef map<string , subInfo > _hash; void DeKey(const char* key,int & t, int & t2){ memcpy(&t,key,sizeof(int)); memcpy(&t2,key+sizeof(int),sizeof(int)); } int main(){ _hash hs; _hash::iterator find; int tid=1; for(int i=1;i<10;i++){ char *key = new char[8]; memset(key,0,8); memcpy(key,(char*)&tid,4); memcpy(key+sizeof(int),(char*)&i,4); cout<<"size:"<<sizeof(key)<<endl; cout<<"leng:"<<strlen(key)<<endl; find=hs.find(string(key)); if(find == hs.end()){ subInfo sub((float)i); hs.insert(make_pair(string(key),sub)); cout<<"key:["<<key<<"]inserted"<<endl; }else { int j=0,k=0; DeKey(find->first.c_str(),j,k); cout<<"key:["<<j<<k<<"] founded"<<endl; delete[] key; } } cout<<"----------------------------------------------------"<<endl; cout<<"hash size:"<<hs.size()<<endl; cout<<"----------------------------------------------------"<<endl; return 0; }
// Q2. when i finished inserting iterm, release key-memory ?




Reply With Quote