我想填充一个集合,其中每个项目都是 8 个字节(可以是 8 个可读字符或二进制数据)。好像是
std::unordered_set<char [8]> d;
d.insert("abcdefgh");
std::set<char [8]> d2;
d2.insert("abcdefgh");
std::string s = "Hello this is a string, the first 16 char will be inserted in the set";
d2.insert(s.substr(0,16));
没用。为什么?错误似乎是,除其他外,
The C++ Standard doesn't provide a hash for this type.
(对于 unordered_set
案例)。
有没有办法制作一组 8 字节的项目?
注意:我想避免 std::unordered_set<std::string> d
因为这里每个项目实际上是 8 个数据字节,我想保持简单和小(该集合将有数百万个项目)。我想避免使用另一层内存 std::string
结构本身/指针等
注意 2:我使用的是 set
/unordered_set
,而不是 vector 或其他任何东西,因为我想要超快速的成员资格查找。
最佳答案
我建议使用 std::array<char, 8>
相反;
你必须定义一个散列器:
struct hash_char_8 {
std::size_t operator()(std::array<char, 8> arr) {
}
}
或者只使用 std::set
.
并且您需要将字面量转换为数组:
#include <algorithm>
// std::copy
std::array<char, 8> to_char_8(char const literal[9]) {
std::array<char, 8> a;
std::copy(literal, literal + 8, a.begin());
return a;
}
使用它的代码:
std::unordered_set<std::array<char, 8>, hash_char_8> d;
d.insert(to_char_8("abcdefgh"));
关于c++ - 如何填充项目为 8 个字符的集合? (std::set<字符[8]>),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48139731/