簡體   English   中英

數據正確放置在數組中,但在打印數組時不存在

[英]Data placed into array correctly but is not there when array is printed

我正在實現自己的哈希表,並且遇到了以下問題:當我將節點插入表中時,在遍歷數組時不會打印出它們。 我使用數組數組作為基礎數據結構,其邏輯如下:

  • 我將節點傳遞給插入函數。 該函數基於我節點中的數據類型,調用C ++ STL提供的適當的哈希函數。
  • 然后,我修改哈希表的大小返回的哈希值,並使用該值確定放置節點的數組。
  • 我還有一個布爾數組數組(與哈希表大小相同),用於檢查哈希表中的特定位置是否已包含數據。
  • 如果是這樣,我只是不斷循環直到找到一個空白點。

就像我之前說過的那樣,問題在於數據已正確輸入到數組中(我已經用print語句檢查過),但是當我打印數組時,什么也沒輸出。 我還檢查了我的對象是否正確構造(再次使用打印語句),但是一切都很好。 我在下面包含了完整的代碼。 任何幫助將不勝感激!

        ///////START OF NODE.H///////////
        #ifndef NODE_H
        #define NODE_H
        #include <iostream>
        template <typename T>
        class HashTable;

        template <typename T>
        class Node
        {
            friend class HashTable<T>;

            private:
                T data;

            public:
                Node(T Data): data(Data)
                {
                    std::cout << "In the node constructor" << std::endl;
                }

                Node() 
                {
                    decltype(data) {};
                }

                T getData() const
                {
                    return data;
                }
        };
        #endif
        //////////////////////END OF NODE.H////////////////////




        /////START OF HASHTABLE.H///////
        #ifndef HASHTABLE_H
        #define HASHTABLE_H
        #include "Node.h"
        #include <iostream>
        #include <array>
        #include <functional>
        #include <typeinfo>
        #include <string>
        const int TABLE_SIZE=5;
        template <typename T>
        class HashTable
        {
            private:
                std::array<std::array<Node<T>, TABLE_SIZE>, TABLE_SIZE> hashTable;
                std::array<std::array<bool, TABLE_SIZE>, TABLE_SIZE> spots;

            public:
                HashTable()
                {
                    for(int index=0;index<spots.size();++index)
                    {
                        for(int position=0;position<spots.at(index).size();++position)
                        {
                            spots.at(index).at(position)=false;
                        }
                    }
                }

                int hashFunction(Node<T> Node)
                {
                    auto key=Node.getData();

                    std::hash<decltype(Node.getData())> hash_function {};

                    int hash=hash_function(key);

                    if(hash < 0)
                    {
                        hash*=-1;
                    }

                    //std::cout << "The hash value return by the STL hash function for the key " << key << " is " << hash << std::endl;

                    if(hash > TABLE_SIZE)
                    {
                        hash%=TABLE_SIZE;
                    }

                    std::cout << "The hash value for the key " << key << " is " << hash << std::endl;

                    return hash;
                }

                void insert(Node<T> Node)
                {
                    int hashValue=hashFunction(Node);

                    auto location=hashTable.at(hashValue);

                    std::cout << "Going to insert " << Node.getData() << std::endl;

                    for(int index=0;index<location.size();++index)
                    {
                        if(spots.at(hashValue).at(index)==false)
                        {
                            std::cout << "Found a spot that is not taken!" << std::endl;

                            std::cout << "The size of the data at the spot in the array before we insert is: " << location.at(index).getData().size() << std::endl;

                            location.at(index)=Node;

                            std::cout << "The size of the data at the spot in the array after we insert is: " << location.at(index).getData().size() << std::endl;

                            std::cout << "The data that is in the spot in the array: " << location.at(index).getData() << std::endl;

                            std::cout << std::endl;

                            spots.at(hashValue).at(index)=true;

                            break;
                        }
                    }
                }

                bool contains(Node<T> Node)
                {
                    int hashValue=hashFunction(Node);

                    auto location=hashTable.at(hashValue);

                    auto result=find_if(begin(location), end(location), [Node] (const auto & element) {return element.getData()==Node.getData();});

                    if(result!=end(location))
                    {
                        return true;
                    }

                    return false;
                }

                int getSize() const
                {
                    int size {};

                    for(int index=0;index<hashTable.size();++index)
                    {
                        size+=hashTable.at(index).size();
                    }

                    return size;
                }

                void print()
                {
                    std::cout << "In the print function" << std::endl;

                    for(int index=0;index<hashTable.size();++index)
                    {
                        //std::cout << hashTable.at(index).size() << std::endl;

                        for(int position=0;position<hashTable.at(index).size();++position)
                        {                   
                            std::cout << hashTable.at(index).at(position).getData().size() << std::endl;
                        }
                    }

                    /*
                    for(int index=0;index<spots.size();++index)
                    {
                        for(int position=0;position<spots.at(index).size();++position)
                        {
                            if(spots.at(index).at(position)==true)
                            {
                                std::cout << "There should be some data here" << std::endl;
                            }
                        }
                    }
                    */
                }
        };
        #endif
        ////////////END OF HASHTABLE.H//////////



       ////////////START OF MAIN.CPP///////////
             #include "HashTable.h"
             #include <cstdlib>
             #include <random>
             #include <algorithm>
             using namespace std;                 
             int main()
             {
                HashTable<string> hash_table;

                hash_table.insert(Node<string>("Java"));

                hash_table.insert(Node<string>("C++"));

                hash_table.insert(Node<string>("C#"));

                hash_table.insert(Node<string>("Latex"));

                hash_table.insert(Node<string>("Python"));

             }
        /////////////END OF MAIN.CPP/////////////

這些行的insert(Node<T> Node)函數中有一個錯誤:

    auto location=hashTable.at(hashValue);
    //...
    location.at(index) = Node;

location應該是參考,而不是副本。 發生的情況是您要更改本地location ,而不是哈希表使用的實際location 因此,您的任何更改都不會“堅持”。

上面的行應為:

    auto& location=hashTable.at(hashValue);  // <-- note that auto is a reference
    //...
    location.at(index) = Node;  

現在,您將返回的引用分配給一個引用。

另外,我強烈建議您使用調試器,因為如果您單步執行代碼以查看正在執行的操作,則很容易診斷出該錯誤。

HashTable::insert以下行:

auto location = hashTable.at(hashValue);

制作一個Node的副本。 然后,您可以操作並存儲在副本中,而不是hashTable的節點。 引用節點

auto & location = hashTable.at(hashValue);

應該修復它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM