如何填充项目为 8 个字符的集合?(std::set<char [8]>)
How to fill a set where items are 8 char? (std::set<char [8]>)
我想填写一个集合,其中每个项目是 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));
不行。为什么?错误似乎是,除其他外,
C++标准不为此类型提供哈希。
(对于unordered_set
情况(。
有没有办法制作一组 8 字节的项目?
注意:我想避免std::unordered_set<std::string> d
,因为这里每个项目实际上是 8 个数据字节,我想保持简单和小(集合将有数百万个项目(。我想避免std::string
结构本身/指针等使用的另一层内存。
注意2:我使用的是set
/unordered_set
,而不是向量或其他任何东西,因为我想超快的成员资格查找。
我建议改用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"));
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 为什么我无法更改"set<set>"循环中的值<int>
- 对于set上的循环-获取next元素迭代器
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- 在声明中合并两个常量"std::set"(不是在运行时)
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将 std::set 与基于键的比较器一起使用
- 如何使用set实现无序数据结构?
- 使用运算符调用 void 函数时出错<set>
- 修改"std::set"中用户定义类型的值
- 生成提升::hana::set 的常量表达式问题
- 如何在构造函数参数中初始化"std::set"?
- 如何使用 lower_bound/upper_bound 从 std::set 获取索引号?
- 如何在 C++ 中转发声明 std::set?
- 重构使用动态强制转换的 std::set 的比较运算符
- set::find 查找不存在的元素
- 为什么 std::set.erase(first, last) 会影响从中获取 (first, last) 的容器?
- 是std :: set&lt; std :: future&gt;不可能存在