gpt4 book ai didi

c++ - 计算哈希值、整数类型与结构/类

转载 作者:搜寻专家 更新时间:2023-10-30 23:50:16 24 4
gpt4 key购买 nike

我想知道原始整型如int64_t和pod类型如struct { int16_t v[ 4]; };。 int128_t 与 struct {int32_t v[4];} 怎么样?

我知道这将特定于实现,所以我的问题最终与 gnu 标准库有关。谢谢

我发现这个链接很有用 How can I use a custom type for keys in a boost::unordered_map?

最佳答案

std::map不是哈希表。它通常实现为平衡二叉树。你要的是std::unordered_map *.

对于 std::unordered_map , C++ 只定义了内部类型和常见类型的散列值** 例如std::string .您需要为 struct { int16_t v[4]; }; 实现哈希函数你自己。您可以将此结构转换为 int64_t在计算中,则哈希速度不会有任何差异。


(*: == std::tr1::unordered_map == boost::unordered_map__gnu_cxx::hash_map == stdext::hash_map 等)

(**: 20.8.16 类模板 hash : ... 整数类型 (3.9.1)、浮点类型 (3.9.1)、指针类型 (8.3.1) , 和 std::string , std::u16string , std::u32string , std::wstring , std::error_code , std::thread::id , std::bitset , std::vector<bool> .)

关于c++ - 计算哈希值、整数类型与结构/类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2439647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com