c – Google的dense_hash_map在set_empty_key()函数中崩溃

我正在尝试使用google dense_hash_map存储键值数据而不是std:map.

当我使用(int,int)对测试时,我设置了set_empty_key(mymap,-2)并且它有效.

但是,现在当我将它与我的(散列,值)对一起使用时,我设置了set_empty_key(mymap -2)或set_empty_key(mymap,some_random_hash),在这两种情况下我的程序在set_empty_key();中崩溃.

任何人都可以指导我吗?我该如何解决这次崩溃?

谢谢.

解决方法

我不知道你遇到崩溃的确切原因,但根据你的描述,我发现至少有两个潜在的错误.

第一.检查key_type和data_type类型是否为POD类型,并且不包含指向自身的指针.更具体地说(original):

Both key_type and data_type must be
plain old data. In addition,there should
be no data structures that point
directly into parts of key or value,
including the key or value itself (for
instance,you cannot have a value like
struct {int a = 1,*b = &a}. This is
because dense_hash_map uses malloc()
and free() to allocate space for the
key and value,and memmove() to
reorganize the key and value in
memory.

第二.关于使用dense_hash_map.您需要设置一些特殊的“空”键值,该值永远不会用于存储在您的集合中的真实元素.此外,如果您要使用erase(),则需要为已删除的项目指定特殊键,这些键也永远不会用作实际存储项目的键.
完全描述了here

dense_hash_map requires you call set_empty_key() immediately after constructing the hash-map,and before calling any other dense_hash_map method. (This is the largest difference between the dense_hash_map API and other hash-map APIs. See implementation.html for why this is necessary.) The argument to set_empty_key() should be a key-value that is never used for legitimate hash-map entries. If you have no such key value,you will be unable to use dense_hash_map. It is an error to call insert() with an item whose key is the “empty key.” dense_hash_map also requires you call set_deleted_key() before calling erase(). The argument to set_deleted_key() should be a key-value that is never used for legitimate hash-map entries. It must be different from the key-value used for set_empty_key(). It is an error to call erase() without first calling set_deleted_key(),and it is also an error to call insert() with an item whose key is the “deleted key.”

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...