我正在从一个网站上阅读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];
像这样在方括号前加星号是什么意思?
new HashEntry*[TABLE_SIZE]
分配并构造一个TABLE_SIZE
元素数组,其中每个元素都是a HashEntry*
,即指向a的指针HashEntry
。
一个更现代的C ++版本将是:
private:
std::vector<std::unique_ptr<HashEntry>> table;
public:
HashMap() : table(TABLE_SIZE) {}
这样可以避免定义自己的析构函数,并且通常更安全。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句