C++++++s unordered_map / hash_map / Google+s dendense_hash - 如何输入二进制数据(buf+len)和插入操作
我有两个关于Google的dense_hash_map的问题,可以使用它而不是更标准的 unordered_map 或 hash_map:
如何使用任意二进制数据内存段作为键:我想要一个 buffer+length 对,它可能仍包含一些 NUL (\0) 字符。我可以看到如何使用以 NUL 结尾的 char * string ,但这不是我想要的。
- 实现一个操作,查看某个键是否存在,如果不存在,则将其插入,如果它确实返回指向现有键的指针,并让我知道实际发生了什么。
如果有人能阐明这个主题,我将不胜感激。
问候,
--什洛米鱼
I have two questions about Google's dense_hash_map, which can be used instead of the more standard unordered_map or hash_map:
How do I use an arbitrary binary data memory segment as a key: I want a buffer+length pair, which may still contain some NUL (\0) characters. I can see how I use a NUL-terminated char * string , but that's not what I want.
How do I implement an operation where I look if a key exists, and if not - insert it and if it does return the pointer to the existing key and let me know what actually happened.
I'd appreciate it if anyone can shed any light on this subject.
Regards,
-- Shlomi Fish
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不同意尼尔的观点。
我会使用
insert
来表示数字 2。使用find
然后insert
会导致 2 次查找,而使用insert
则会导致 1 次查找如果元素已经存在,则进行查找而不覆盖。通常,插入返回一个迭代器到对应的键/值对(即使没有更新)+一个布尔值,指示插入是否发生。I would disagree with Neil.
I would use
insert
for number 2. Usingfind
theninsert
causes 2 look-ups while usinginsert
causes one look-up without overriding if the element is already present. Normally,insert
returns an iterator to the key/value pair corresponding (even if not updated) + a boolean which indicates whether or not the insertion took place.对于 #1,使用 std::string 作为键 - std::string 可以毫无问题地包含嵌入的 NUL 字符。对于#2,请参阅 Matthieu 的答案。
For #1, use a
std::string
as the key - std::strings can contain embedded NUL characters with no problems. For #2, see Matthieu's answer.