[英]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.