一个hash_map使用错误
2006-07-20 16:44
190 查看
一个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());
}
};
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使用错误
- map[]的正确使用:自以为是导致map的一个错误使用
- dense_hash_map使用过程中一些错误信息
- 在vs中使用hash_map的一个小总结
- 关于包含头文件而编译时却一个未定义错误的原因(未使用命名名字空间)
- spring使用@value关于<context:component-scan base-package=”com.baidu” />的一个错误
- ActiveMQ使用STOMP协议的一个错误问题:Unexpected ACK received for message-id
- 一个linaro 1304 log使用编译错误
- MS-VC 使用MAP文件快速定位错误行 .
- 改变一个list<Map<String,Object>>,并给他重新赋值,不能使用新循环,要使用旧循环
- hash_map使用自定义类型做主键
- 使用JSON报的一个错误java.lang.ClassNotFoundException
- 操作必须使用一个可更新的查询;HTTP 500 - 内部服务器错误; 您未被授权查看该页;您不具备使用所提供的凭据查看该目录或页的权限;HTTP 错误 401.3 - 访问被资源 ACL 拒绝
- map的一个使用技巧
- 使用NexentaStor创建一个iSCSI Target并MAP一个LUN
- C++ 中标准库 map 和 hash_map 的使用方法
- Hash_map的使用
- 模拟大数据处理、linux下hash_map()使用
- hash_map/unordered_map原理和使用整理
- 创建一个简单的本地缓存(使用map)