gpt4 book ai didi

c++ - 我如何使用 boost::thread::id 作为 unordered_map 的键?

转载 作者:太空狗 更新时间:2023-10-29 20:01:10 26 4
gpt4 key购买 nike

根据documentation , 一个 boost::thread::id可以被认为对于每个正在运行的线程都是唯一的,并且可以在容器中使用,例如 std::setstd::map (因为 < 运算符被 thread::id 覆盖)。

我的问题是我想使用 thread::id作为 boost::unordered_map 的关键,但是它要求 key 是“可散列的”(即支持散列到 size_t )。由于 thread::id 的所有实现细节都被隐藏了,我认为我没有任何可以使用的东西。

所以我的问题是 - 是否可以使用 thread::id 作为 unordered_map 的键?

最佳答案

您可以使用流媒体功能:

struct Hasher
{
size_t operator()(const boost::thread::id& id)
{
std::ostringstream os; os << id; return hash(os.str());
}
};

类(class)的一小段摘录,以便其他人可以看到什么是可能的:

class thread::id
{
public:
id();

bool operator==(const id& y) const;
bool operator!=(const id& y) const;
bool operator<(const id& y) const;
bool operator>(const id& y) const;
bool operator<=(const id& y) const;
bool operator>=(const id& y) const;

template<class charT, class traits>
friend std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, const id& x);
};

关于c++ - 我如何使用 boost::thread::id 作为 unordered_map 的键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2850142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com