#pragma once
#include <hash_map>
#include <string>
#include <iostream>
struct MyStringComp
{
//define hash function for strings
enum
{
//parameters for hash table
bucket_size = 4,
min_buckets = 8
};
size_t operator()(const std::string & s1) const
{
//hash string s1 to size_t value
const unsigned char * p = (const unsigned char *)s1.c_str();
size_t hashval = 0;
for (size_t n = s1.size();0 < n;--n)
hashval += *p++; //or whatever
return (hashval);
}
bool operator()(const std::string & s1, const std::string & s2) const
{
if (s1.compare(s2) < 0)
{
return true;
}
else
{
return false;
}
}
};
int main(int arg,char * argv[])
{
stdext::hash_map<std::string,int, MyStringComp> mapStringTable;
mapStringTable.insert(std::pair<std::string,int>("13136",5));
mapStringTable.insert(std::pair<std::string,int>("16633",10));
mapStringTable.insert(std::pair<std::string,int>("16733",10));
mapStringTable.insert(std::pair<std::string,int>("16833",10));
mapStringTable.insert(std::pair<std::string,int>("16933",10));
mapStringTable.insert(std::pair<std::string,int>("16033",10));
stdext::hash_map<std::string,int, MyStringComp>::iterator It;
It = mapStringTable.find("16833");
if (It != mapStringTable.end())
{
std::cout<<It->first;
}
getchar();
return 0;
}
|