hash_map自学笔记

来源:互联网 发布:运营商网络转型 编辑:程序博客网 时间:2024/06/03 14:24

只是一个简单的例程,使用stlport5.21

#include <hash_map>
#include <iostream>
using namespace  std;


template<class Key,class NameType,class YearType,class Addrtype>
struct StudentRecord_tag{
struct StudentInfo_tag{
NameType name;
YearType year;
Addrtype addr;
};
typedef Key IdType;
typedef StudentInfo_tag StudentInfo;


IdType id;
StudentInfo sf;


};
int main()
{
hash_map<const char*,float> hm;
hm["orange"]=3.45f;
hm["lichee"]=5.67f;
hm["banana"]=3.89f;
cout<<hm["banana"]<<endl;
hm.erase("lichee");
pair<const char*,float> p("pear",8.9f);
hm.insert(p);
hash_map<const char*,float>::iterator i,iend;
iend=hm.end();
for (i=hm.begin();i!=iend;i++)
{
cout<<(*i).first<<" "<<(*i).second<<endl;
}
cout<<hm.bucket_count()<<endl;
cout<<hm.size()<<endl;


cout<<endl;
typedef StudentRecord_tag<int,char*,int,char*> StudentRecord;
StudentRecord sr[]={
{192,"李强",28,"武汉"},
{168,"月月",34,"广州"},
{123,"王青",54,"成都"}
};
hash_map<StudentRecord::IdType,StudentRecord::StudentInfo> hms;
for (int j=0;j<3;j++)
{
hms[sr[j].id]=sr[j].sf;
}
hash_map<StudentRecord::IdType,StudentRecord::StudentInfo>::iterator ii,iiend;
iiend=hms.end();
for (ii=hms.begin();ii!=iiend;ii++)
{
cout<<(*ii).first<<" "
<<(*ii).second.name<<" "
<<(*ii).second.year<<" "
<<(*ii).second.addr<<endl;
}
cout<<endl;
ii=hms.find(192);
if (ii!=iiend)
{
cout<<(*ii).first<<" "
<<(*ii).second.name<<" "
<<(*ii).second.year<<" "
<<(*ii).second.addr<<endl;
}
else
{
cout<<"failed!"<<endl;
}
system("PAUSE");
return 0;
}



原创粉丝点击