C++++++s unordered_map / hash_map / Google+s dendense_hash - 如何输入二进制数据(buf+len)和插入操作

发布于 2024-09-04 18:42:23 字数 455 浏览 3 评论 0原文

我有两个关于Google的dense_hash_map的问题,可以使用它而不是更标准的 unordered_map 或 hash_map:

  1. 如何使用任意二进制数据内存段作为键:我想要一个 buffer+length 对,它可能仍包含一些 NUL (\0) 字符。我可以看到如何使用以 NUL 结尾的 char * string ,但这不是我想要的。

  2. 实现一个操作,查看某个键是否存在,如果不存在,则将其插入,如果它确实返回指向现有键的指针,并让我知道实际发生了什么。

如果有人能阐明这个主题,我将不胜感激。

问候,

--什洛米鱼

I have two questions about Google's dense_hash_map, which can be used instead of the more standard unordered_map or hash_map:

  1. 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.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一百个冬季 2024-09-11 18:42:23

我不同意尼尔的观点。

我会使用 insert 来表示数字 2。使用 find 然后 insert 会导致 2 次查找,而使用 insert 则会导致 1 次查找如果元素已经存在,则进行查找而不覆盖。通常,插入返回一个迭代器到对应的键/值对(即使没有更新)+一个布尔值,指示插入是否发生。

std::pair<iterator, bool> result = map.insert(std::make_pair(key(), value());

result.first->second; // accesses the value at key: `key()`

I would disagree with Neil.

I would use insert for number 2. Using find then insert causes 2 look-ups while using insert 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.

std::pair<iterator, bool> result = map.insert(std::make_pair(key(), value());

result.first->second; // accesses the value at key: `key()`
一杯敬自由 2024-09-11 18:42:23

对于 #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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文