如何在C 中正确使用HASH_SET
How to correctly use hash_set in C++
我正在尝试创建一个 hash_set
来保存不同文件的名称,例如:
struct eq {
bool operator()(const char* c1, const char* c2) {
return strcmp(c1, c2) == 0;
}
};
int main(int argc, char* argv[])
{
hash_set<const char*, hash<const char*>, eq> fileNames;
return 0;
}
这给我带来了很多编译器错误:
Error 1 error C2039 : 'bucket_size' : is not a member of 'std::hash<const char *>' C : Program Files(x86)xDevMicrosoft Visual Studio 12.0VCincludexhash 264 1 Tests
Error 2 error C2065 : 'bucket_size' : undeclared identifier C : Program Files(x86)xDevMicrosoft Visual Studio 12.0VCincludexhash 264 1 Tests
Error 3 error C2039 : 'value_type' : is not a member of 'eq' C : Program Files(x86)xDevMicrosoft Visual Studio 12.0VCincludexmemory0 419 1 Tests
Error 4 error C2146 : syntax error : missing ';' before identifier 'value_type' C : Program Files(x86)xDevMicrosoft Visual Studio 12.0VCincludexmemory0 419 1 Tests
Error 5 error C4430 : missing type specifier - int assumed.Note : C++ does not support default - int C : Program Files(x86)xDevMicrosoft Visual Studio 12.0VCincludexmemory0 419 1 Tests
Error 6 error C2602 : 'std::allocator_traits<_Alloc>::value_type' is not a member of a base class of 'std::allocator_traits<_Alloc>' C :Program Files(x86)xDevMicrosoft Visual Studio 12.0VCincludexmemory0 419 1 Tests
Error 7 error C2146 : syntax error : missing ',' before identifier 'value_type' C : Program Files(x86)xDevMicrosoft Visual Studio 12.0VCincludexmemory0 242 1 Tests
Error 8 error C2065 : 'value_type' : undeclared identifier C : Program Files(x86)xDevMicrosoft Visual Studio 12.0VCincludexmemory0 242 1 Tests
Error 9 error C2059 : syntax error : '>' C : Program Files(x86)xDevMicrosoft Visual Studio 12.0VCincludexmemory0 242 1 Tests
...
hash_set
是Visual Studio的STL扩展中的弃用类型。它需要与您提供的模板参数不同。
您实际使用的是(或多或少)与您的参数一起使用)是std::unordered_set
:
#include <cstring>
#include <unordered_set>
using namespace std;
struct eq {
bool operator()(const char* c1, const char* c2) {
return strcmp(c1, c2) == 0;
}
};
int main(int argc, char* argv[])
{
unordered_set<const char*, hash<const char*>, eq> fileNames;
return 0;
}
除此之外,我强烈建议使用std::string
而不是const char*
,这将使您的代码降低至:
#include <unordered_set>
#include <string>
int main(int argc, char* argv[])
{
std::unordered_set<std::string> fileNames;
}
还请看到这个问题,为什么使用const char*
作为std::unordered_map
的关键是一个坏主意。本质上,您还必须提供自己的哈希功能并照顾钥匙的分配和交易。
相关文章:
- 什么是"#include <boost/functional/hash.hpp> "?
- 为什么我无法更改"set<set>"循环中的值<int>
- 对于set上的循环-获取next元素迭代器
- 在声明中合并两个常量"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::hash<uint64_t>
- 为什么 std::set.erase(first, last) 会影响从中获取 (first, last) 的容器?
- 将 std:set<int32_t> 复制到 std::set <uint32_t>的好方法
- 错误 C2676:std::set::const_iterator 没有运算符 + 函数?
- std::set 是否将对象连续存储在内存中?