首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

一个hash_map的编译有关问题

2012-04-21 
一个hash_map的编译问题新人,不知hash_map的用法,以下这段代码再G++下编译错误,可是看不懂错误说明,求各位

一个hash_map的编译问题
新人,不知hash_map的用法,以下这段代码再G++下编译错误,可是看不懂错误说明,求各位指教,谢谢!

C/C++ code
#include <cstdio>#include <cstdlib>#include <hash_map>#include <cstring>#include <algorithm>using namespace std;using namespace __gnu_cxx;class Addr{public:    char add[6];    Addr()    {        add[0] = '\0';    }    Addr(const Addr& x);    inline Addr& operator=(const Addr& x);};Addr::Addr(const Addr& x){    strcpy(add,x.add);}Addr& Addr::operator=(const Addr& x){    strcpy(add,x.add);    return *this;}inline bool operator== (const Addr& x,const Addr& y){    if(strcmp(x.add,y.add) == 0)        return true;    return false;}inline bool operator!= (const Addr& x,const Addr& y){    return !(x == y);}struct str_hash   {      size_t operator()(const Addr& str) const      {      size_t __h(0);        for(size_t i = 0;i < strlen(str.add);++ i)              __h =  __h + str.add[i];          return __h;      }  };  int main(){    Addr b1,b2;    int n;    char c;    scanf("%s %s %d",b1.add,b2.add,&n);    hash_map<Addr,Addr> record;    for(int i = 0;i < n;++ i)    {        Addr t1,t2;        scanf("%s %c %s",t1.add,&c,t2.add);        record.insert(make_pair(t1,t2));    }    Addr* w1;    Addr* w2;    w1 = new Addr[n];    w2 = new Addr[n];    int c1(0),c2(0);    w1[c1] = b1;    ++ c1;    w2[c2] = b2;    ++ c2;    while(strcmp(b1.add,"-1") != 0)    {        b1 = record[b1];        w1[c1] = b1;        ++ c1;    }    strcpy(w1[c1].add,"-1");    while(strcmp(b2.add,"-1") != 0)    {        b2 = record[b2];        w1[c2] = b2;        ++ c2;    }    strcpy(w2[c2].add,"-1");    while(c1 >= 0 && c2 >= 0 && w1[c1] == w2[c2])    {        -- c1;        -- c2;    }    ++ c1;    printf("%s",w1[c1].add);}

出错原因好像在于59行的insert,可能是自己写的类没实现完整。

[解决办法]
hash_map<Addr,Addr> record;

==>
hash_map<Addr,Addr,str_hash > record;

热点排行