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,這位大神寫的比較詳細,仔細看看。