unordered_map

将pair作为unordered_map的key,需要重写hash

 struct pair_hash{
        template<class T1,class T2>
        size_t operator()(const pair<T1,T2> &pair) const{
            return hash<T1>()(pair.first)^hash<T2>()(pair.second);
        }
    };
    unordered_map<pair<string,string>,bool,pair_hash> tempMap;

使用unordered_map需要注意

Count函数
size_type count ( const key_type& key ) const
count函数用以统计key值在unordered_map中出现的次数。实际上,c++ unordered_map不允许有重复的key。因此,如果key存在,则count返回1,如果不存在,则count返回0.

//出现则返回1,否则返回0
     if(tempMap.count(pair(s1,s2))) 
            return tempMap[make_pair(s1,s2)];
//此方法检查key-value,若目前key不存在,则会在判断完之后加入tempMap[pair(s1,s2]=false,使得size加一
     if(tempMap[pair(s1,s2]==true) 
            return true;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值