[英]Hashmap initializing dynamic array of nodes with nullptr c++
我在使用 nullptr 正确初始化节点的动态数组时遇到了问题。
HashMap::HashMap(int size)
{
this->sizeOfArray = size;
this->hashArray = new Node*[this->sizeOfArray];
for (int i = 0; i < this->sizeOfArray; i++)
{
hashArray[i] = nullptr;
}
}
这就是我的“hashArray”在标题中的样子。
Node **hashArray;
forloop 完成了所有 500 个循环,但是当我查看数组中的数据时,我只能在得到“无法读取内存”之前看到第一个元素。
这是我的意思的图像https://ibb.co/d0GLw9
这是节点的样子
Node()
{
value = 0;
}
Node(std::string input)
{
key = input;
value = 1;
next = nullptr;
}
Node(std::string input, int count)
{
key = input;
this->value = count;
next = nullptr;
}
Node(std::string input, int count, Node *next)
{
key = input;
this->value = count;
this->next = next;
}
Node *next;
std::string key;
unsigned int value;
我怀疑这个问题导致我以后无法向 hashArray 添加任何新节点。
你所有的 500 个Node
指针都是NULL
,但是hashArray
的类型是Node **
,这就是为什么调试器只显示一个元素,就好像它是一个指向一个Node
指针的指针。 换句话说,由于您的数组是动态的,调试器不知道要显示多少个元素。
您遇到的错误与查看第一个指针为NULL
Node
的内容有关,这自然无法读取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.