相关疑难解决方法(0)

tr1 :: hash for boost :: thread :: id?

我开始使用命名空间中的unordered_settr1来加速对普通(基于树的)STL的访问map.但是,我想在boost(boost::thread::id)中存储对线程ID的引用,并意识到这些标识符的API是如此不透明,以至于您无法清楚地获得它的哈希值.

令人惊讶的是,boost实现了tr1(包括hashunordered_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());
    }
};
Run Code Online (Sandbox Code Playgroud)

也就是说,序列化它,将哈希应用于结果字符串.但是,这似乎比实际使用STL效率低map<boost::thread::id>.

所以,我的问题:您是否找到了更好的方法?在boost和tr1中是否明显不一致而不强迫hash<boost::thread::id>类的存在?

谢谢.

c++ hash boost boost-thread unordered-set

9
推荐指数
1
解决办法
2706
查看次数

标签 统计

boost ×1

boost-thread ×1

c++ ×1

hash ×1

unordered-set ×1