VS2012关于hash_map的使用简略

Posted mfmdaoyou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VS2012关于hash_map的使用简略相关的知识,希望对你有一定的参考价值。

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的使用简略的主要内容,如果未能解决你的问题,请参考以下文章

使用 hash_map 时在 stl 字符串上使用的最佳散列算法是啥?

使用 hash_map 时在 stl 字符串上使用的最佳散列算法是啥?

vs code 自定义代码片段

VS2015使用技巧 打开代码片段C#部分

vs 2010代码片段

vs 2010代码片段