【发布时间】:2024-06-03 11:25:02
【问题描述】:
我开始使用tr1 命名空间中的unordered_set 类来加速对普通(基于树的)STL map 的访问。但是,我想在 boost (boost::thread::id) 中存储对线程 ID 的引用,并意识到这些标识符的 API 是如此不透明,以至于您无法清楚地获得它的哈希值。
令人惊讶的是,boost 实现了 tr1 的一部分(包括 hash 和 unordered_set),但它没有定义能够散列线程 ID 的散列类。
查看boost::thread::id 的文档,我发现线程 ID 可以输出到流中,所以我的哈希解决方案是:
struct boost_thread_id_hash
{
size_t operator()(boost::thread::id const& id) const
{
std::stringstream ostr;
ostr << id;
std::tr1::hash<std::string> h;
return h(ostr.str());
}
};
即对其进行序列化,将哈希应用于结果字符串。但是,这似乎比实际使用 STL map<boost::thread::id> 效率低。
所以,我的问题是:您找到更好的方法了吗?不强制 hash<boost::thread::id> 类的存在是否在 boost 和 tr1 中存在明显的不一致?
谢谢。
【问题讨论】:
标签: c++ boost hash boost-thread unordered-set