使用向量的向量创建一个尺寸

Creating a hashtable using vectors of vectors?

本文关键字:向量 一个 创建      更新时间:2023-10-16

我当前正在尝试编写一个创建哈希表的程序,该程序使用向量的向量进行collision分辨方法。

我面临的问题是,在运行时,创建了向量的向量,但是所有尺寸剩余的输入向量。我知道我的put函数是错误的,但我不知道在哪里/为什么。<<<<<<<<

这是我第一次创建哈希桌子,我感谢您在问题上的任何帮助。我的目标是创建一个输入向量的向量,每个条目都有其关联的键和价值。找到新条目键的哈希值后,它应该检查输入向量的键值,以查看键是否已经存在。如果确实如此,它会更新该密钥的值。

这是table.cpp的段:

Table::Table(unsigned int maximumEntries) : maxEntries(100){
    this->maxEntries = maximumEntries;
    this->Tsize = 2*maxEntries;
}
Table::Table(unsigned int entries, std::istream& input){ //do not input more than the specified number of entries.
    this->maxEntries = entries;
    this->Tsize = 2*maxEntries;
    std::string line = "";
    int numEntries = 0;

    getline(input, line);
    while(numEntries<maxEntries || input.eof()){ // reads to entries or end of file
        int key;
        std::string strData = "";
        convertToValues(key, strData, line);
        put(key, strData); // adds each of the values to the tab;e
        numEntries++;
        getline(input,line);
    }
}

void Table::put(unsigned int key, std::string data){ 
    Entry newEntryObj(key,data); //create a new Entry obj
    put(newEntryObj);
}

void Table::put(Entry e){ // creating the hash table
    assert(currNumEntries < maxEntries);
    int hash = (e.get_key() % Tsize);
    Entry newEntry = Entry(e.get_key(), e.get_data()); 
    for(int i = 0; i < hashtable[hash].size(); i++){
        if (e.get_key() == hashtable[hash][i].get_key()){ 
            hashtable[hash][i].set_data(e.get_data());
        }
        else{
            hashtable[hash].push_back(newEntry);  // IF KEY DOESNT EXIST, ADD TO THE VECTOR
        }
    }
}

这是table.h

#ifndef table_h
#define table_h
#include "entry.h"
#include <string>
#include <istream>
#include <fstream>
#include <iostream>
#include <vector>

class Table{
  public: 
    Table(unsigned int max_entries = 100); //Builds empty table with maxEntry value
    Table(unsigned int entries, std::istream& input); //Builds table designed to hold number of entires

    void put(unsigned int key, std::string data); //creates a new Entry to put in
    void put(Entry e);  //puts COPY of entry into the table
    std::string get(unsigned int key) const; //returns string associated w/ param, "" if no entry exists
    bool remove(unsigned int key); //removes Entry containing the given key
    friend std::ostream& operator<< (std::ostream& out, const Table& t); //overloads << operator to PRINT the table.
    int getSize();
    std::vector<std::vector<Entry>> getHashtable(); 

  private:
    std::vector<std::vector<Entry>> hashtable; //vector of vectors
    int Tsize; //size of table equal to twice the max number of entries
    int maxEntries;
    int currNumEntries;
#endif /* table_h */
};

和entry.h:

#include <string>
#include <iosfwd>
class Entry {
public:
    // constructor
    Entry(unsigned int key = 0, std::string data = "");
    // access and mutator functions
    unsigned int get_key() const;
    std::string get_data() const;
    static unsigned int access_count();
    void set_key(unsigned int k);
    void set_data(std::string d);
    // operator conversion function simplifies comparisons
    operator unsigned int () const;
    // input and output friends
    friend std::istream& operator>>
    (std::istream& inp, Entry &e);
    friend std::ostream& operator<<
    (std::ostream& out, Entry &e);
private:
    unsigned int key;
    std::string data;
    static unsigned int accesses;
};

您的代码有各种各样的问题,但是您的问题的答案是:

void Table::put(Entry e){ // creating the hash table

看看循环。

for(int i = 0; i < hashtable[hash].size(); i++){

现在,hashtable[hash]是向量。但是最初它没有任何元素。所以hashtable[hash].size()是0。因此,您不输入循环。

最重要的是,首先尝试访问hashtable[hash]会导致不确定的行为,因为hashtable无法正确调整到Tsize大小。在构造函数中尝试一下:

this->maxEntries = maximumEntries;
this->Tsize = 2*maxEntries;
this->hashtable.resize(this->Tsize);

编辑:

如果您使用std::vector::at功能而不是std::vector::operator[],则更容易理解。例如:

void Table::put(Entry e){ // creating the hash table
    assert(currNumEntries < maxEntries);
    int hash = (e.get_key() % Tsize);
    Entry newEntry = Entry(e.get_key(), e.get_data()); 
    for(int i = 0; i < hashtabl.at(hash).size(); i++){
        if (e.get_key() == hashtable.at(hash).at(i).get_key()){ 
            hashtable.at(hash).at(i).set_data(e.get_data());
        }
        else{
            hashtable.at(hash).push_back(newEntry);  // IF KEY DOESNT EXIST, ADD TO THE VECTOR
        }
    }
}

无需调整hashtable大小,此代码在第一次尝试执行hashtable.at(hash)时会抛出out_of_range

P.S。这些都没有测试。