问题描述
我可以在 C++ 中创建一个 unordered_set 向量吗?像这样
Can I create a unordered_set of vectors in C++? something like this
因为我知道使用 std lib 的set"类是可能的,但似乎它不适用于无序版本谢谢
because I know that is possible with the "set" class of the std lib but seems that it doesn't work for the unordered version thanks
更新:这正是我要使用的代码
Update: this is the exactly code that I'm trying to use
如果我使用set就可以了,但是在尝试使用无序版本时我收到一个编译错误
and it's all right if I use set, but I receive a compilation error when try to use the unordered version
推荐答案
当然可以.不过,您必须提出一个哈希值,因为默认值 (std::hash
) 将不会实现.例如,基于这个答案,我们可以构建:
Sure you can. You'll have to come up with a hash though, since the default one (std::hash<std::vector<int>>
) will not be implemented. For example, based on this answer, we can build:
然后:
如果您愿意,您也可以为这种类型的 std::hash
添加专门化(注意这可以em> 是 std::vector
的未定义行为,但对于用户定义的类型绝对没问题):
You could also, if you so choose, instead add a specialization to std::hash<T>
for this type (note this could be undefined behavior with std::vector<int>
, but is definitely okay with a user-defined type):
这篇关于C++ unordered_set 向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!