stdext::hash_map</STRING,INT>
1.问题
使用stdext::hash_map<std::string,int>在有的平台上编译通不过的问题
相应的解决方法是:
#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;
}无法识别 ext/hasp_map 的解决
最新推荐文章于 2021-07-15 12:43:19 发布
本文介绍了一种使用自定义哈希函数解决stdext::hash_map在不同平台上的编译问题的方法,并提供了一个具体的示例程序。
713

被折叠的 条评论
为什么被折叠?



