C++中的hash_set是如何工作的

How does hash_set in C++ work?

本文关键字:工作 何工作 hash 中的 set C++      更新时间:2023-10-16

我不知道如何在C++中使用hash_set。我对这种语言非常陌生,所以我不知道如何做很多事情。如何使用SGI hash_set扩展,使编译器最终编译时不会出错?这是我的头文件:

#ifndef _GAME1_H
#define _GAME1_H
#include "card.h"
#include "deck.h"
#include <ext/hash_set>
const unsigned int TRIALS = 10;
class Game1 {
private:
    // Card::Value is defined in card.h as a public enum:
    // enum Value { NullCard, Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King };
    std::hash_set<Card::Value> *map; // does this really need to be a pointer?
public:
    Game1();
    bool isPair();
    bool isFlush();
    void returnToDeck();
};
#endif

当我尝试编译时,我得到:

In file included from game1.cpp:9:
game1.h:13: error: using-declaration for non-member at class scope
game1.h:13: error: expected `;' before '<' token
make: *** [game1.o] Error 1
  1. 我不知道"在类范围内为非成员使用声明"是什么意思
  2. 当我基本上遵循SGI在他们网站上的相同示例时,为什么编译器会抱怨"在'<'标记之前应为';'"
  3. 我正在使用gcc 3.4.6,因此无法使用unordered_set
  4. 我看了一个简单的C++hash_set示例,但我不明白他们为什么使用hash<int> H;,这有关系吗

我陷入了僵局,因为在咨询谷歌数小时后,我真的无法弄清楚这一点。

我认为您应该将map声明为

// answering your other question, most likely it doesn't have to be a pointer.
__gnu_cxx::hash_set<Card::Value> map;

(根据hash_set来源)

此外,map不是一个好的变量名称,因为它是一个标准类的名称。尽管这不应该是编译错误的原因。