添加元素<字符串、指针>以提升无序哈希图
Adding an element <string, pointer> to boost unordered hashmap
我在C++中使用了boost无序哈希图,但我无法将元素添加到我的哈希图中(我的程序有分段错误)。我对C++很陌生,我的大部分代码(哈希映射处理除外)都是 C 代码。你能指出问题吗?
// my simplified code
struct Record
{
char *data;
};
typedef boost::unordered_map<std::string, std::vector<Record*> > MAP;
typedef std::pair<std::string,std::vector<Record*> > PAIR;
struct OuterRelation
{
short num_keys;
short join_key_ndx;
MAP hash_table;
};
OuterRelation *outer_relation = (OuterRelation *) malloc (sizeof(OuterRelation)) ;
Record *new = (Record *) malloc(sizeof(Record));
new->data = "somestring";
outer_relation->hash_table[new->data].push_back(new);
问题出在最后一行。
停止使用 malloc
。正确的语法是:
OuterRelation *outer_relation = new OuterRelation;
您对malloc
的使用已经为外部关系结构单独分配了足够的空间。如果结构仅包含普通旧数据,这可能就足够了。但是,hash_table
成员是一个类,使用 malloc
使其未初始化。
new
(最基本的)是malloc
和对new
'd 对象的构造函数的调用的组合。结构的构造函数将依次调用其成员的构造函数,包括映射。映射的构造函数将初始化其数据成员。
您还需要停止使用 new
作为变量名称。这与new
C++关键字冲突。
相关文章:
- 使用哈希图进行相对排序
- 使用哈希图打印第一个重复元素;
- 我想使用哈希图打印非重复的数字;
- 如何按值对无序哈希映射进行排序
- 是否有任何内置的哈希图函数来处理 c++ 中的值输入?
- 无法迭代哈希图C++
- 如何提升::序列化矢量的哈希图
- 什么更快,内联函数调用或哈希图查找
- 添加元素<字符串、指针>以提升无序哈希图
- 哈希图常规修改
- 更改哈希图的Qt信号
- 少量项目的哈希图
- 使用哈希图 c++ 查找最常用的单词
- 扩展哈希图
- 更改 std::p air 类型会影响哈希图
- c ++我可以在我的代码中将这样的向量新到哈希图中吗?
- 如何提高哈希图迭代的复杂性
- 自定义哈希图给出在 Linux 上使用 gcc 的编译错误
- 将数据并行添加到哈希图
- 哈希图实现:---哈希码