1

我正在从网站上阅读 C++ 中的哈希表实现示例并看到了这个。

private:
  HashEntry **table;
public:
  HashMap() {
        table = new HashEntry*[TABLE_SIZE];
        for (int i = 0; i < TABLE_SIZE; i++)
              table[i] = NULL;
  }

我不明白的语法是:

table = new HashEntry*[TABLE_SIZE];

像这样的括号前有星号是什么意思?

4

2 回答 2

5

new HashEntry*[TABLE_SIZE]分配并构造一个元素数组TABLE_SIZE,其中每个元素都是 a HashEntry*,即指向 a 的指针HashEntry

一个更现代的 C++ 版本是:

private:
  std::vector<std::unique_ptr<HashEntry>> table;
public:
  HashMap() : table(TABLE_SIZE) {}

这避免了必须定义自己的析构函数,并且通常更安全。

于 2015-02-05T06:02:51.153 回答
0

星号表示它是一个指针

这里有一些链接

http://www.cprogramming.com/tutorial/c/lesson6.html

http://www.tutorialspoint.com/cplusplus/cpp_pointers.htm

http://www.augustcouncil.com/~tgibson/tutorial/ptr.html

于 2015-02-05T06:12:55.313 回答