VS关于hash_map使用的一些常用构造方法汇总,包括基本类型和结构体,相信够一般模仿使用:
# include<hash_map> #include<iostream> #include<string> struct order { char orderNO[20]; char name[10]; int NO; char type; }; typedef order Order; struct cmp{ enum { bucket_size=100, }; size_t operator()(Order order1) { return sizeof(order1.orderNO)+sizeof(order1.name)+sizeof(order1.NO)+sizeof(order1.type); } bool operator()(Order order1,Order order2) { if(strcmp(order1.orderNO,order2.orderNO)>0) return true; else if(strcmp(order1.orderNO,order2.orderNO)<0) return false; else { if(strcmp(order1.name,order2.name)>0) return true; else if(strcmp(order1.name,order2.name)<0) return false; else{ if(order1.NO>order2.NO)return true; else if(order1.NO<order2.NO)return false; else{ if(order1.type>order2.type)return true; else return false; } } } } }; struct CharLess : public std::binary_function<const char*,const char*, bool> { public: result_type operator()(const first_argument_type& _Left, const second_argument_type& _Right) const { return(stricmp(_Left, _Right) < 0 ? true : false)<span style="font-family: Arial, Helvetica, sans-serif;">;</span> } }; struct CharLess1 : public std::binary_function<char*, char*, bool> { public: result_type operator()(const first_argument_type& _Left, const second_argument_type& _Right) const { return(stricmp(_Left, _Right) < 0 ? true : false); } }; void testHashMap() { stdext::hash_map<int,int> mhasp_map1; for(int i=0;i<9;i++) mhasp_map1.insert(stdext::pair<int,int>(i,i+10)); stdext::hash_map<int,int>::iterator mit1=mhasp_map1.begin(); while(mit1!=mhasp_map1.end()) { std::cout<<mit1->first<<" "<<mit1->second<<std::endl; mit1++; } stdext::hash_map<const char*,std::string,std::hash_compare<const char *,CharLess> > testhash; testhash["东方不败"]="葵花宝典,技压群雄"; testhash["西门吹雪"]="寒剑三尺,削铁如泥"; testhash["上官玉儿"]="花容月貌,一笑倾城"; stdext::hash_map<const char *,std::string,std::hash_compare<const char *,CharLess> >::iterator testhashit=testhash.begin(); while(testhashit!=testhash.end()) { std::cout<<testhashit->first<<" "<<testhashit->second.c_str()<<std::endl; testhashit++; } char a[20]; scanf("%s",a); std::cout<<testhash.find(a)->first<<std::endl; stdext::hash_map<char *,std::string,std::hash_compare<char *,CharLess1> > testhash1; testhash1["东方不败"]="葵花宝典,技压群雄"; testhash1["西门吹雪"]="寒剑三尺,削铁如泥"; testhash1["上官玉儿"]="花容月貌,一笑倾城"; stdext::hash_map<char *,std::string,std::hash_compare<char *,CharLess1> >::iterator testhashit1=testhash1.begin(); while(testhashit1!=testhash1.end()) { std::cout<<testhashit1->first<<" "<<testhashit1->second<<std::endl; testhashit1++; } //char a[20]; scanf("%s",a); std::cout<<testhash.find(a)->first<<std::endl; //std::cout<<testhash1.find("东方不败")->first<<std::endl; stdext::hash_map<std::string,std::string> testhash2; testhash2["东方不败"]="葵花宝典,技压群雄"; testhash2["西门吹雪"]="寒剑三尺,削铁如泥"; testhash2["上官玉儿"]="花容月貌,一笑倾城"; stdext::hash_map<std::string,std::string>::iterator testhashit2=testhash2.begin(); while(testhashit2!=testhash2.end()) { std::cout<<testhashit2->first<<" "<<testhashit2->second<<std::endl; testhashit2++; } scanf("%s",a); std::string str(a); std::cout<<testhash2.find(str)->first<<std::endl; Order order1={"SR405","小麦",100,10}; Order order2={"SR406","小麦",100,10}; Order order3={"SR406","小米",100,10}; Order order4={"SR406","小米",101,10}; Order order5={"SR406","小米",101,11}; stdext::hash_map<Order,std::string,cmp> teststruct; teststruct.insert(std::pair<Order,std::string>(order1,"sr112")); teststruct.insert(std::pair<Order,std::string>(order2,"sr113")); teststruct.insert(std::pair<Order,std::string>(order3,"sr114")); teststruct.insert(std::pair<Order,std::string>(order4,"sr115")); teststruct.insert(std::pair<Order,std::string>(order5,"sr116")); stdext::hash_map<Order,std::string,cmp>::iterator it; it=teststruct.find(order1); std::cout<<it->second<<std::endl; g<span style="font-family: Arial, Helvetica, sans-serif;">etchar();</span> } int main() { testHashMap(); getchar(); }
如果有不明确的地方,请参考博客:http://blog.csdn.net/sdhongjun/article/details/4517325,这位大神写的比较详细,仔细看看。
VS2012关于hash_map的使用简略,布布扣,bubuko.com
时间: 2024-12-16 05:31:27