一个hash_map使用错误
g++的 hash_map 运行不起来
#include <string>
#include <ext/hash_map>
using namespace std;
using namespace __gnu_cxx;
namespace __gnu_cxx
{
template<> struct hash<const string>
{
size_t operator()(const string& s) const
{ return hash()(s.c_str()); }
};
template<> struct hash<string>
{
size_t operator()(const string& s) const
{ return hash()(s.c_str()); }
};
}
int main( void )
{
hash_map<string,int> a;
a["abc"] = 1; // 这一句一执行的话,程序直接退出
system("pause");
}
该段代码由 周星星
贴于 http://blog.vckbase.com/jzhang/archive/2006/03/28/18807.html
试运行,确实会崩溃。
经debug跟踪,很快就能找到错误来源。
原来是
template<> struct hash<string>::operator()(const string &)
定义成了无穷递归。
如下修正:
template<> struct hash<string>
{
size_t operator()(const string& s) const
{
return __stl_hash_string(s.c_str());
}
};
//#####################################################
hash_map不在C++98/2003标准中,因此在VC++2005和g++中使用的方法略有区别。
【1】VC++2005
#include <hash_map> // 注意头文件和namespace
using namespace stdext;
int main()
{
hash_map<string, int> hmap;
return 0;
}
【2】g++
#include<string>
#include <ext/hash_map>
using namespace std;
using namespace __gnu_cxx;
// 需要自己写hash函数
struct string_hash
{
size_t operator()(const string& str) const
{
return __stl_hash_string(str.c_str());
}
};
int main()
{
hash_map<string, int, string_hash> hmap;
return 0;
}
分享到:
相关推荐
关于hash_map的用法与解释: #include <hash_map> #include #include using namespace std; //define the class class ClassA{ public: ClassA(int a):c_a(a){} int getvalue()const { return c_a;} void ...
hash_map
linux 下hash_map的基本原理及使用,希望对大家有帮助。
代码重点是hash_table,附加std::map与其做对比,实现的是一条sql语句:select c_nationkey, c_mktsegment, count(*), max(c_acctbal) from aaa_customer_1g group by c_nationkey, c_mktsegment order by c_...
TBB 并发容器 concurrent_queue concurrent_vector concurrent_hash_map TBB 并发容器 concurrent_queue concurrent_vector concurrent_hash_map TBB 并发容器 concurrent_queue concurrent_vector concurrent_hash_...
一个用Hash算法实现的map,可以实际项目所使用。 希望能帮助大家学习。
本实例实现了一个hash_map,key是string类型,即可以存储索引是string的数据,希望对大家有帮助
jg :: dense_hash_map 一个简单的std::unordered_map替代品,具有更好的性能,但失去了稳定的寻址方式,这是一种折衷方案。 在此处查看此哈希图的详细说明: : 生成状态: 特拉维斯(Travis):
结合Java的HashMap中的一些优点,改进了C++ 的hash_map。 详细说明见我的博客:http://blog.csdn.net/mdj67887500/article/details/6907702
hash_map基于hash table(哈希表)。 哈希表最大的优点,就是把数据的存储和查找消耗的时间大大降低,几乎可以看成是常数时间;而代价仅仅是消耗比较多的内存。然而在当前可利用内存越来越多的情况下,用空间换时间...
但是对于hash表来说,由于无法避免re-hash所带来的性能问题,即使大多数情况下hash表的性能非常好,但是re-hash所带来的不稳定性在当时是不能容忍的。 不过由于hash表的性能优势,它的使用面还是很广的,于是第三方...
Hash值查看以及修改软件(Hash_1.0.4_0523.exe以及HashModifier.exe),网盘必备工具。
STL hash_map: 链式散列 版权所有 (c) 2014,龙 (Ryan) 南宫。 保留所有权利。 邮箱: 创建时间:2014 年 7 月 15 日 这是无序的哈希映射,它具有恒定的插入、删除、搜索时间,并支持向前/向后迭代。 hash_map 的...
MD5校验Hash_1.0.4 轻松复制各个参数 检验软件安全性
文件MD5查看 Hash_1.0.4_XiaZaiBa.exe
asp版hmac_sha1加密方式,支持中英文,真正和PHP的hash_hmac加密结果完全一样,与java、php、python、js 一致,附对比。支持中文utf-8编码
Hash_1.0.4.exe检验文件Hash,
stm32f407平台上实现的hash算法
哈希计算器,可以计算文件的MD5、SHA1和CRC32值。它支持文件拖放,速度很快