微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/c/118257.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐