gpt4 book ai didi

c++ - 使用 vector 的 vector 创建哈希表?

转载 作者:行者123 更新时间:2023-11-28 05:07:34 30 4
gpt4 key购买 nike

我目前正在尝试编写一个创建哈希表的程序,使用 vector 中的 vector 作为我的冲突解决方法。

我面临的问题是,在运行时,一个 vector 的 vector 被创建,但里面的所有入口 vector 的大小仍然为 0。我知道我的 put 函数有问题,但我不知道在哪里/为什么。

这是我第一次创建哈希表,如果您能就问题所在提供任何帮助,我将不胜感激。我的目标是创建一个由 Entry vector 组成的 vector ,每个 Entry 都有其关联的键和值。找到新 Entry 键的哈希值后,它应该检查 Entry vector 的键值以查看该键是否已经存在。如果是,它会更新该键的值。

这是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] 是一个 vector 。但最初它没有任何元素。所以 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 异常。

附言这些都没有经过测试。

关于c++ - 使用 vector 的 vector 创建哈希表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44319406/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com