返回   cpper编程论坛 > 算法
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

回复
 
LinkBack (5) 主题工具 显示模式
  5 links from elsewhere to this Post. Click to view. #1 (permalink)  
旧 2008-06-24
初级会员
 
注册日期: 2008-06-24
住址: 广东深圳
帖子: 1
xzp1st 正向着好的方向发展
发送 MSN 消息给 xzp1st
默认 求一Hash算法

请问各位大虾,存在一个快速的从字符串到连续整数的映射函数吗?谢谢!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2008-06-25
liuxinyu 的头像
高级会员
 
注册日期: 2006-02-09
帖子: 303
文章: 48
liuxinyu 正向着好的方向发展
默认 回复: 求一Hash算法

是不是我没有理解“连续整数”的含义。有不少算法,最简单的就是这个:

代码:
#include <iostream> #include <string> int hash(const std::string& str){ int res=0; for(std::string::const_iterator it=str.begin(); it!=str.end(); ++it) res=res*256+(*it); return res; } int main(int, char**){ std::cout<<hash(std::string("hello"))<<"\n"; std::cout<<hash(std::string("world")); }
liuuuxin@WEIFANG ~/temp
$ g++ str_hash.cpp

liuuuxin@WEIFANG ~/temp
$ ./a
1701604463
1869769828
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用

LinkBacks (?)
LinkBack to this Thread: http://www.cpper.com/c/t4878.html
作者 For Type 日期
&#26377;&#27809;&#26377;&#26742;&#31354;&#38388;&#25928;&#29575;&#26356;&#39640;&#30340;&#23383;&#31526;&#20018;Hash - TopLanguage | Google Groups This thread Refback 2008-06-26 12:11 PM
Untitled document This thread Refback 2008-06-26 02:44 AM
&#26377;&#27809;&#26377;&#26742;&#31354;&#38388;&#25928;&#29575;&#26356;&#39640;&#30340;&#23383;&#31526;&#20018;Hash - TopLanguage | Google Groups This thread Refback 2008-06-26 12:44 AM
&#26377;&#27809;&#26377;&#26742;&#31354;&#38388;&#25928;&#29575;&#26356;&#39640;&#30340;&#23383;&#31526;&#20018;Hash - TopLanguage | Google Groups This thread Refback 2008-06-25 08:55 PM
&#26377;&#27809;&#26377;&#26742;&#31354;&#38388;&#25928;&#29575;&#26356;&#39640;&#30340;&#23383;&#31526;&#20018;Hash - TopLanguage | Google Groups This thread Refback 2008-06-25 08:51 PM


所有时间均为格林尼治时间 +9。现在的时间是 12:42 PM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2008,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0