c++上的哈希聚合
Hash Aggregate on C++
我试图在c++中实现一个散列聚合算法。
下面是伪代码:for each input row
begin
calculate hash value on group by column(s)
check for a matching row in the hash table // calculate hash function **(A)**
if we do not find a match
insert a new row into the hash table // calculate hash function again? **(B)**
else
update the matching row with the input row
end
output all rows in the hash table
使用STL (c++), A行是:iter_type it=groupByMap.find(hashKey);
所以我将支付一个查找,计算哈希值。
行B将是:it = groupByMap.insert(it, newHashElement);
所以我支付再次查找,计算哈希值。
是否有办法只执行一次哈希计算?
假设groupByMap
为std::unordered_map<YOUR_KEY, YOUR_VALUE>
,类型定义为YourMap
。
如果是,只需:
std::pair<YourMap::iterator, bool> position =
groupByMap.emplace(hashKey, newHashElement);
if (!position.second)
{
// Insertion didn't occur, a key was already there, so just update contents.
YourUpdate(*position.first);
}
在上面的代码中,我使用了std::unordered_map
的emplace()
方法。groupByMap.emplace(hashKey, newHashElement)
本质上是groupByMap.insert(std::make_pair(hashKey, newHashElement))
,但没有创建临时pair(它就地构建pair)。所以,如果你没有使用std::unordered_map
,但其他类,如stl::hash_map
,没有emplace()
,你可以用insert()
代替emplace()
描述,它会正常工作。
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 使用Qt C++计算类似Git的SHA1哈希
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 用C++将哈希表写入文件并从文件中恢复
- c++找不到具有相同哈希的无序集合元素
- 哈希文件递归并保存到矢量Cryptopp中
- 对 pair<pair<int,int>pair<int,int unordered_map进行哈希处理>>
- 直接在 unordered_map 的方法中使用哈希,而不是生成哈希的用户定义对象
- 如何为字符串生成唯一但一致的 N 位哈希(小于 64 位)?
- 使用对象的基类部分模板专用化对对象进行哈希处理::哈希
- boost::包含提升单元的元组的哈希值
- 使用 Key 对 C++ 中的哈希映射进行排序. 无法排序
- C++中的并发哈希表
- 哈希映射使用 nullptr c++ 初始化节点的动态数组
- 在具有开放寻址的哈希表中插入节点 [优化逻辑]
- 与C++哈希表的基础知识混淆
- 如何为位集找到/实现一个好的哈希函数
- 对于短字符串来说,这是一个很好的哈希函数吗?
- 调整大小和复制哈希表数组中的元素