VS2012关于hash_map的使用简略

本文详细介绍了VS中hash_map的多种构造方法及使用实例,包括基本类型和结构体的运用,并通过具体示例展示了如何进行插入、查找等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/sdhongjun/article/details/4517325,这位大神写的比较详细,仔细看看。

redisTemplate怎么实现public static void main(String[] args) { // 创建Redis连接 try (Jedis jedis = new Jedis(REDIS_HOST, REDIS_PORT)) { // 认证(如果有密码) if (REDIS_PASSWORD != null) { jedis.auth(REDIS_PASSWORD); } // 选择数据库 jedis.select(REDIS_DATABASE); // 测试连接 try { jedis.ping(); System.out.println("成功连接到Redis服务器"); } catch (JedisException e) { System.err.println("无法连接到Redis: " + e.getMessage()); return; } // 使用管道批量导入 Pipeline pipeline = jedis.pipelined(); long startTime = System.currentTimeMillis(); HashMap<String, LiveUserListVO> hashMap = new HashMap<>(); for (int i = 1; i <= TOTAL_RECORDS; i++) { try { // 生成13位时间戳(1600000000000~1700000000000) long id = 1600000000000L + (long)(random.nextDouble() * 100000000000L); // 构建数据对象 LiveUserListVO online = LiveUserListVO.builder() .id(id) .name(faker.name().lastName()) .avatar(String.format("https://example.com/avatar/%d.png?rnd=%.4f", random.nextInt(1000), random.nextDouble())) .type(5) .status("Online") .micStatus(0) .muteVoice(false) .build(); // 转换为JSON并导入Redis hashMap.put(String.valueOf(online.getId()), online); } catch (Exception e) { System.err.println("生成第 " + i + " 条记录时出错: " + e.getMessage()); } } pipeline.hset(REDIS_KEY, hashMap); // 确保最后一批数据提交 pipeline.sync(); long elapsed = System.currentTimeMillis() - startTime; System.out.printf("导入完成!共 %,d 条数据,耗时 %.2f 秒%n", TOTAL_RECORDS, elapsed / 1000.0); } catch (Exception e) { System.err.println("Redis操作发生错误: " + e.getMessage()); e.printStackTrace(); } }
最新发布
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值