newplan

阿基米德在洗澡時發現浮力原理,高興得來不及穿㆖褲子,跑到街㆖大喊:Eureka(我找到了)。
posts - 39, comments - 26, trackbacks - 0, articles - 4
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

STL HASH_MAP简单应用

Posted on 2008-05-05 17:17 山泉弯延 阅读(14512) 评论(6)  编辑 收藏 引用
/*
 *用来测试STL hash_map 
 *简单例子2008.5.5
*/
#include  
<cstdlib>
#include  
<iostream>
#include  
<string>
#include  
<hash_map.h>/*因为hash_map暂不为CPP标准所以没办法写为<hash_map>*/
/*-------------------------------------------*/
using  std::cout;
using  std::endl;
using  std::string;
/*-------------------------------------------*/
/*函数类
 *作为hash_map的hash函数 
 *string没有默认的hash函数 
 
*/ 
class str_hash{
      
public:
       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_map的比较函数 )
 *(查找的时候不同的key往往可能对用到相同的hash值
*/ 
class str_compare
{
      
public:
             
bool operator()(const string& str1,const string& str2)const
             {
return   str1==str2;}
};
/*-------------------------------------------*/
int 
main(
int argc, char *argv[])
{  
    hash_map
<string,string,str_hash,str_compare>  myhash;
    
    myhash[
"google"]="newplan";
   
    myhash[
"baidu"]="zhaoziming";
   
    
if(myhash.find("google")!=myhash.end())
      cout
<<myhash["google"]<<endl;
    
    system(
"PAUSE");
    
    
return EXIT_SUCCESS;
}
/*-------------------------------------------*/

Feedback

# re: STL HASH_MAP简单应用  回复  更多评论   

2008-08-12 12:58 by 陈盛
我在.net2005上怎么运行不了你的代码?编译不通过,说:
错误 1 fatal error C1083: Cannot open include file: 'hash_map.h': No such file or directory f:\private\vc++\testhashmap\testhashmap\testhashmap.cpp 8
另外,我想问一下,怎么根据keyvalue对hash_map进行排序。
我的邮箱:crasyman@163.com,能否把你的可执行的工程传给我?

# re: STL HASH_MAP简单应用  回复  更多评论   

2008-09-27 17:16 by rubby
fatal error C1083: Cannot open include file: 'hash_map.h': No such file or directory

# re: STL HASH_MAP简单应用  回复  更多评论   

2008-12-08 14:38 by xiong
你好! 我是在vc6下想用hashmap ,要加stlport 但还是不能用。能告诉我你是怎样让hashmap能用的吗?谢谢你啦~我邮箱xiongzhanying@126.com

# re: STL HASH_MAP简单应用  回复  更多评论   

2008-12-13 19:10 by 山泉弯延
我的运行环境是DEV,你可以子DEV的安装包中找到该文件HASH_MAP.H,至于其他的IDE我还没有去考虑过,不过你可以马上查@xiong

# re: STL HASH_MAP简单应用[未登录]  回复  更多评论   

2009-04-24 08:48 by kevin
at VS 2005,

#include <iostream>
#include <string>
#include <hash_map>

using namespace stdext;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
hash_map<string,string> myhash;

myhash["google"]="newplan";

myhash["baidu"]="zhaoziming";

if(myhash.find("google")!=myhash.end())
cout<<myhash["google"]<<endl;

system("PAUSE");

return EXIT_SUCCESS;
}

# re: STL HASH_MAP简单应用  回复  更多评论   

2009-09-29 23:58 by fall
VS2008环境下即使添加了using namespace stdext;这句话好像还是不行。
添加了上面那句话后错误信息更多,而且是hash函数内部的错误,请教楼主一下,这个问题该怎么解决?谢谢

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理