无法迭代boost无序哈希映射
Unable to iterate through boost unordered hashmap
我正在使用boost无序哈希表,我很难循环遍历哈希表中的所有键。
#include <vector>
#include <iostream>
#include <boost/unordered_map.hpp>
#include <boost/foreach.hpp>
struct Record
{
char *data;
};
typedef boost::unordered_map<std::string, std::vector<Record*> > MAP;
struct OuterRelation
{
short num_keys;
short join_key_ndx;
MAP hash_table;
};
Record *new_record = malloc(sizeof(Record));
new_record->data = "some string";
char *key = new_record->data;
(outer_relation->hash_table)[key].push_back(new_record);
/* print all keys in the hash table */
BOOST_FOREACH(MAP::value_type pair, outer_relation->hash_table)
{
std::string key = pair.first;
...
}
程序在foreach循环时失败。我添加到哈希表中的元素有问题吗?
由于这是c++ ,请停止使用malloc
。(c++使用new
和delete
。虽然:也不要用这些。使用std::make_unique
,或者std::make_shared
或std::vector<T>
)。
说到这里,原因如下:C没有类。C只有pod类型struct
s。
只要你在c++代码中保证这一点,你就可以"摆脱"C-isms。下面是一个c++泛型函数,它通过检查:
来实现malloc技巧template <typename T, typename... Args>
T* make_memory_leak(Args&&... args) {
static_assert(std::is_pod<T>::value, "undefined behaviour for non-POD types");
T* raw = static_cast<T*>(malloc(sizeof(T)));
static_assert(boost::proto::is_aggregate<T>::value, "aggregate initialization required");
*raw = { std::forward<Args>(args)... };
return raw;
}
现在你可以直接写
auto record_ptr = make_memory_leak<Record>("some string");
和
等价Record* record_ptr = static_cast<Record*>(malloc(sizeof(Record)));
*record_ptr = { "some string" }; // aggregate initialization
所以,这是你的测试代码,工作:Live on Coliru
#include <vector>
#include <iostream>
#include <boost/unordered_map.hpp>
#include <boost/foreach.hpp>
#include <boost/proto/traits.hpp>
#include <cstdlib>
struct Record {
char const* data;
};
typedef boost::unordered_map<std::string, std::vector<Record*> > MAP;
struct OuterRelation
{
short num_keys;
short join_key_ndx;
MAP hash_table;
};
template <typename T, typename... Args>
T* make_memory_leak(Args&&... args) {
static_assert(std::is_pod<T>::value, "undefined behaviour for non-POD types");
T* raw = static_cast<T*>(malloc(sizeof(T)));
static_assert(boost::proto::is_aggregate<T>::value, "aggregate initialization required");
*raw = { std::forward<Args>(args)... };
return raw;
}
int main()
{
auto outer_relation = std::make_shared<OuterRelation>();
for (auto key : { "some string", "some other string", "which", "by the way", "are", "const char(&)[]" })
outer_relation->hash_table[key].push_back(make_memory_leak<Record>(key));
/* print all keys in the hash table */
BOOST_FOREACH(MAP::value_type pair, outer_relation->hash_table)
{
std::cout << pair.first << "n";
}
}
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 使用 Key 对 C++ 中的哈希映射进行排序. 无法排序
- 哈希映射使用 nullptr c++ 初始化节点的动态数组
- 如何按值对无序哈希映射进行排序
- 哈希映射如何在 c++ 中实现?
- C++中的哈希映射等效
- 以字符串向量作为值的哈希映射
- 来自并发哈希映射的迭代器是否安全
- 如何在这个哈希映射中实现 AVL 树
- 无法迭代结构"string"的哈希映射:[列表] C++
- 什么是最快的解决方案,用于读/写入哈希映射
- 可以安全地依赖unordered_map中的初始化值(哈希映射)
- 使用按插入方式排序的参数创建哈希映射
- 哈希映射没有可行的重载 '='
- 使用分配器对哈希映射进行高效的序列化和反序列化
- 由于动态数组而导致的哈希映射内存泄漏
- 为boost哈希映射定义自定义哈希函数
- 是我们迭代时更改哈希映射的行为,已定义
- 哈希映射和有序遍历
- 哈希映射错误:与调用 '(const __gnu_cxx::.