栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++11以前中的hash

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++11以前中的hash

自己杜撰一个题目:

对于一组数据,存在数组中a,

现在输入多行,每行两个整数m,n

要求输出每个a【m,n】中最大值。

每次输入一对m,n就在hash_map中查找是否存在,若存在,则直接输出,若不错在则去a中查找,查找过程中产生的中间结果都加以记录。这样后面若遇到查过的,则不用再查了。
————————————————

#include  
#include 
using namespace std;
//using namespace stdext;  
struct node
{
    int i,j;
};

struct myhash {//自定义哈希函数 

    ssize_t operator()(const node &p) const 
    {
         __gnu_cxx::hash()(p.i) + __gnu_cxx::hash()(p.j);
    }
};

struct myequalto{//判断相等时的函数 

    ssize_t operator()(const node &p,const node &q) const 
    {
        return p.i==q.i&&p.j==q.j;
    }
};
__gnu_cxx::hash_map myMap;
int myfind(int a[],int m,int n)
{//在a[m,n]区间内求最大值
    if(m==n) 
    {
        return myMap[{m,n}]=a[m];
    }
    if(myMap[{m,n}])return myMap[{m,n}];
    
    return myMap[{m,n}]=max(a[m],myfind(a,m+1,n));
    
    
}

int main()  
{  
    __gnu_cxx::hash_map hm;
    
    int a[]={1,3,5,2,4,6} ,m,n;;
    while(cin>>m>>n)
    {
        node t={m,n};
        if(myMap.find(t)!=myMap.end())
        {
            cout<         }
        else
        {
            myMap[{m,n}]=myfind(a,m,n);
            cout<         }
    }
    
    return 0;  
}  


在SGI STL中,提供了以下hash函数: ```cpp

struct hash

struct hash

struct hash

struct hash

struct hash

struct hash

struct hash

struct hash

struct hash

struct hash

struct hash

也就是说,如果你的key使用的是以上类型中的一种,你都可以使用缺省的hash函数。当然你自己也可以定义自己的hash函数。对于自定义变量,你只能如此,例如对于string,就必须自定义hash函数。例如: ```cpp

struct str_hash
{
    
    size_t operator()(const string& str) const 
    { 
        unsigned long __h = 0; 
        for (size_t i = 0 ; i < str.size() ; i ++) 
            __h = 5*__h + str[i]; 
        return size_t(__h); 
    } 
}; 
//如果你希望利用系统定义的字符串hash函数,你可以这样写: 
struct str_hash

    size_t operator()(const string& str) const 
    { 
        return __stl_hash_string(str.c_str()); 
    } 
};

其他复杂类型,科以此类推

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/311543.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号