只要集合或映射不变,迭代器是迭代boost::unordered_set还是boost::unordered_map以相同的顺序迭代?
我最近才开始关注boost和它的容器,我在网上阅读了一些文章,在stackoverflow上,boost :: unordered_map是大型集合中表现最快的容器.所以,我有这个类State,它必须在容器中是唯一的(没有重复),并且容器中将有数百万甚至数十亿个状态.因此,我一直在尝试优化它以实现小尺寸和尽可能少的计算.之前我正在使用boost :: ptr_vector,但是当我在stackoverflow上读到时,只要没有那么多对象,向量就是好的.在我的情况下,状态描述来自机器人的感觉运动信息,因此可能存在大量状态,因此快速查找是最重要的.在unordered_map 的boost文档之后,我意识到我可以做两件事来加快速度:使用hash_function,并使用相等运算符根据hash_function比较状态.因此,我实现了一个私有hash()函数,它接收状态信息并使用boost :: hash_combine,创建一个std :: size_t哈希值.operator ==基本上比较状态的哈希值.所以:
是std :: size_t足以覆盖数十亿可能的hash_function组合吗?为了避免重复状态,我打算使用他们的hash_values.
在创建state_map时,我应该使用State*或哈希值作为键吗?即:boost::unordered_map<State*,std::size_t> state_map;
或
boost::unordered_map<std::size_t,State*> state_map;
使用boost :: unordered_map :: iterator = state_map.find()的查找时间比通过boost :: ptr_vector并比较每个迭代器的键值更快吗?
最后,任何关于如何优化这种无序地图以获得速度和快速查找的提示或技巧将不胜感激.
编辑:我已经看到了不少答案,一个不使用boost而是使用C++ 0X,另一个不使用unordered_set,但说实话,我还是想看看boost :: unordered_set如何与hash函数一起使用.我已经按照boost的文档进行了实现,但我仍然无法弄清楚如何使用boost的hash函数和有序集.
我想存储boost::gregorian::date为一个键,boost::unordered_map但我无法编译代码,因为它缺少这个类的正确哈希函数.
std::string并存储它.我可能想避免这种解决方案,因为使用字符串非常昂贵.day()函数,我不确定这是否真的合适.有没有其他更好的方法来存储日期或功能导出日期作为数字?
在C#我喜欢的TryGetValue方法,Dictionary因为它允许我在一个调用中确定字典是否包含键和接收值,如果是这样:
Instrument instrument;
if (isinId2Instrument.TryGetValue(isin_id, out instrument))
{
// key exist, instrument contains value
} else {
// key doesn't exist
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做同样的事情boost::unordered_map?
对于我的下一个任务,我需要使用一个非常大的哈希; 因为我有一个旧的编译器,我不能使用C++ 0x std::unordered_map.理想情况下,我需要的是reserve提前为大量物品腾出空间.我找不到这种方法boost::unordered_map:是否有任何地方或功能达到同样的目的?
2个关联容器是相同的; 我可以看到rehash函数和用于控制存储桶数量的相同构造函数,但不能看到有关许多元素的函数.
你能帮帮我吗?
我有以下代码:
boost::unordered_map<std::string, int> map;
map["hello"]++;
map["world"]++;
for(boost::unordered_map<std::string, int>::iterator it = map.begin(); it < map.end(); it++){
cout << map[it->first];
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译时出现以下错误但不知道为什么?
error: no match for ‘operator<’ in ‘it < map.boost::unordered::unordered_map<K, T, H, P, A>::end [with K = std::basic_string<char>, T = int, H = boost::hash<std::basic_string<char> >, P = std::equal_to<std::basic_string<char> >, A = std::allocator<std::pair<const std::basic_string<char>, int> >, boost::unordered::unordered_map<K, T, H, P, A>::iterator = boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<const std::basic_string<char>, int> >*, std::pair<const std::basic_string<char>, int> >]()
Run Code Online (Sandbox Code Playgroud) 我编写了一个程序,需要使用以下库处理非常大的数据:
所以,我有内存问题(该程序使用很多),我想也许我可以替换这个库(已经存在的东西或我自己的实现):
那么,三个问题: