在这段代码中,我找到了 here对于到处都有 NULL 的简单 C++ 哈希,我得到一个
NULL was not declared in this scope.
我正在使用 MinGW 编译器 g++。我的猜测是 NULL 不是保留关键字?我如何确定 gcc 的版本并查看 C++ 关键字的引用列表?
此列表here声明 NULL 不是关键字。
int main()
{
}
const int TABLE_SIZE = 128;
class HashEntry
{
private:
int key;
int value;
public:
HashEntry(int key, int value)
{
this->key = key;
this->value = value;
}
int getKey()
{
return key;
}
int getValue()
{
return value;
}
};
class HashMap
{
private:
HashEntry **table;
public:
HashMap()
{
table = new HashEntry*[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++) table[i] = NULL;
}
int get(int key)
{
int hash = (key % TABLE_SIZE);
while (table[hash] != NULL && table[hash]->getKey() != key) hash = (hash + 1) % TABLE_SIZE;
if (table[hash] == NULL)return -1;
else return table[hash]->getValue();
}
void put(int key, int value)
{
int hash = (key % TABLE_SIZE);
while (table[hash] != NULL && table[hash]->getKey() != key)
hash = (hash + 1) % TABLE_SIZE;
if (table[hash] != NULL) delete table[hash];
table[hash] = new HashEntry(key, value);
}
~HashMap()
{
for (int i = 0; i < TABLE_SIZE; i++)
if (table[i] != NULL) delete table[i];
delete[] table;
}
};
最佳答案
NULL
是在 stddef.h
中声明的标准宏。您需要包含 stddef.h
(或 cstddef
)才能使用 NULL
。
如果您不想包含任何内容,您始终可以使用 0
代替 NULL
。
关于c++ - 什么是NULL,需要声明吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7748133/