C++ STL hash_map 使用自定义的数据类型,自定义哈希表和比较函数,失败。。。求教。。
#include<iostream>#include<hash_map>#include<string>usingnamespacestd;usingnamespaces...
#include <iostream> #include <hash_map> #include <string>
using namespace std; using namespace stdext;
class student{
public: int number; string name;
student(string str_name,int num):number(num),name(str_name){} student(){}
};
struct equalto{
bool operator () (const student & stu1,const student & stu) const{
if((stu1.name==stu.name) && (stu1.number==stu.number)) return true;
else return false; }
};
struct hash_sxl{unsigned long operator() (const string i) const{const unsigned long num=13; unsigned long total=0;total=i*13;return total;}
};
int main(){
hash_map<string,student,hash_sxl,equalto> hmap; student Newstu1;Newstu1.name="sunxiaoliang";
Newstu1.number=1234;student Newstu2;Newstu2.name="tianjinlong";Newstu2.number=765432;
hmap[1234]=Newstu1;
hmap[75432]=Newstu2;
hash_map<string,student,hash_sxl,equalto>::iterator itr=hmap.begin();
for(;itr!=hmap.end();++itr){cout<<itr->first<<" "<<(*itr).second.name<<" "<<itr->second.number<<endl;}
return 展开
using namespace std; using namespace stdext;
class student{
public: int number; string name;
student(string str_name,int num):number(num),name(str_name){} student(){}
};
struct equalto{
bool operator () (const student & stu1,const student & stu) const{
if((stu1.name==stu.name) && (stu1.number==stu.number)) return true;
else return false; }
};
struct hash_sxl{unsigned long operator() (const string i) const{const unsigned long num=13; unsigned long total=0;total=i*13;return total;}
};
int main(){
hash_map<string,student,hash_sxl,equalto> hmap; student Newstu1;Newstu1.name="sunxiaoliang";
Newstu1.number=1234;student Newstu2;Newstu2.name="tianjinlong";Newstu2.number=765432;
hmap[1234]=Newstu1;
hmap[75432]=Newstu2;
hash_map<string,student,hash_sxl,equalto>::iterator itr=hmap.begin();
for(;itr!=hmap.end();++itr){cout<<itr->first<<" "<<(*itr).second.name<<" "<<itr->second.number<<endl;}
return 展开
1个回答
展开全部
你排一下版吧。实在看的费力。
hash_map比较函数返回的应该是一个size_t 值吧。
看看这个
http://stlchina.huhoo.net/twiki/bin/view.pl/Main/STLDetailHashMap
hash_map比较函数返回的应该是一个size_t 值吧。
看看这个
http://stlchina.huhoo.net/twiki/bin/view.pl/Main/STLDetailHashMap
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询