c++ - 如何在C++中正确使用hash_set

标签 c++ error-handling hashset

我正在尝试创建一个 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)\xDev\Microsoft Visual Studio 12.0\VC\include\xhash  264 1   Tests
Error   2   error C2065 : 'bucket_size' : undeclared identifier C : \Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xhash  264 1   Tests
Error   3   error C2039 : 'value_type' : is not a member of 'eq'    C : \Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xmemory0   419 1   Tests
Error   4   error C2146 : syntax error : missing ';' before identifier 'value_type' C : \Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xmemory0   419 1   Tests
Error   5   error C4430 : missing type specifier - int assumed.Note : C++ does not support default - int    C : \Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xmemory0   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)\xDev\Microsoft Visual Studio 12.0\VC\include\xmemory0    419 1   Tests
Error   7   error C2146 : syntax error : missing ',' before identifier 'value_type' C : \Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xmemory0   242 1   Tests
Error   8   error C2065 : 'value_type' : undeclared identifier  C : \Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xmemory0   242 1   Tests
Error   9   error C2059 : syntax error : '>'    C : \Program Files(x86)\xDev\Microsoft Visual Studio 12.0\VC\include\xmemory0   242 1   Tests
...

最佳答案

hash_set 是 Visual Studio 的 STL-extension 中弃用的类型.它需要与您提供的不同的模板参数。

您实际应该使用的(以及将(或多或少)与您的参数一起使用的)是 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;

}

另见 this question ,为什么使用 const char* 作为 std::unordered_map 的键是个坏主意。本质上,您还必须提供自己的哈希函数并负责 key 的分配和解除分配。

关于c++ - 如何在C++中正确使用hash_set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30013344/

相关文章:

c++ - 如何关闭程序中用于打印调试数据的功能

php - PHP的MD5哈希函数返回500内部错误

Java的hashSet处理具有相同哈希码的多个项目

java - 替换 HashSet 内容而不创建新的 HashSet 对象

java - 在 JAVA 中设置工作不正确

c++ - 如何将函数从 C++ 可执行文件公开给 LuaJIT

c# - 如何从 C# 调用 C++ API

c++ - 将 unique_ptr 的 vector 传递给对象。 vector 成为成员变量。正确的做法?

node.js - 当收到 Error 对象时如何停止函数?

.net - 隐藏错误报告窗口