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

前端之家收集整理的这篇文章主要介绍了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.”

原文链接:https://www.f2er.com/c/118257.html

猜你在找的C&C++相关文章