如何使用 boost::thread::id 作为 unordered_map 的键?
我的问题是,我想使用 thread::id
作为 boost::unordered_map
的键,但是它要求该键是“可哈希的”(即支持散列到size_t
)。由于 thread::id 的所有实现细节都被隐藏,我认为没有任何可以使用的东西。
所以我的问题是 - 是否可以使用 thread::id 作为 unordered_map 的键?
According to the documentation, a boost::thread::id
can be considered unique for each running thread and can be used in containers such as std::set
and std::map
(because the <
operator is overridden for thread::id
).
My problem is that I'd like to use thread::id
as a key to an boost::unordered_map
, however it requires that the key is "hashable" (ie. supports hashing to a size_t
). Since all implementation detail for thread::id is hidden I don't think have anything I can use.
So my question is - is it possible to use thread::id as a key to an unordered_map?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用流功能:
该类的小摘录,以便其他人可以看到什么是可能的:
You can use the streaming ability:
Little excerpt of the class, so that others may see what's possible:
你有多少个线程?除非你有超过几百个,否则不太可能
具有大量散列的
unordered_map
(散列非常重,尤其是基于std::stringstream
)会比std::map
更快。不要忘记std::map
具有相当小的常数的日志复杂性。如果您有数百个线程,那么您的应用程序可能存在问题。
How many threads do you have? Unless you have more then several hundred it is unlikely that
unordered_map
with heavy hash (and hash is heavy especially based onstd::stringstream
) would be faster thenstd::map
. Don't forger thatstd::map
has log complexity with quite small constant.And if you have hundreds of threads, then there is probably a problem with your application.
为什么需要继续处理字符串?您可以使用
Why would you need to proceed with strings? You can use
文档说它可以写入流。将其写入
std::ostringstream
并对str()
结果进行哈希处理。尽管输出格式未指定,但它对于给定 ID 来说是唯一的,并且对于程序的给定运行而言是一致的(只要线程 ID 仍然有效)。The documentation says it can be written to a stream. Write it to a
std::ostringstream
and hash thestr()
result. Although the output format is unspecified, it's unique for a given ID and consistent for a given run of your program (which is as long as the thread ID would remain valid anyway).