【问题标题】:Hash table - hash function implementation哈希表——哈希函数实现
【发布时间】:2016-03-25 05:26:28
【问题描述】:

我有这个项目,我需要在其中实现一个哈希表。我有两个班级:粉丝和门票。粉丝可以拥有门票,每张门票都与粉丝的电子邮件相关联。

我的问题是,什么是关键,我应该在哪里实现我的哈希函数?我的猜测是它会在 Ticket.h 上,但我仍然不知道如何将门票与粉丝(所有者)电子邮件相关联。

我认为不需要任何代码,但如果有任何疑问,我会发布一些代码。

最好的问候

类迷(“Adepto”)

class Adepto {

int uid;
unordered_set<string> email;
static int newID;
string nome;
string nEquipa;

公开:

Adepto(string nome);
//Adepto(string nome, Equipa* e1, vector<Bilhete*> bilhetes);
Adepto();

unsigned int getID() const;

string getNome() const;
void setNome(string n);

string getEquipa() const;
void setEquipa(string nEq);

string getEmail() const;
void setEmail(string novoEmail);

机票等级(bilhete)

struct hash_adeptos{
int operator() (const Adepto &a1) const{
    return a1.getEmail()().size(); }

bool operator() (const Adepto & a1, const Adepto & a2) const{
    return a1.getEmail() == a2.getEmail();}

};


typedef tr1::unordered_set<Adepto, hash_adeptos, hash_adeptos> TabelaAdeptos;

 class Bilhete{
TabelaAdeptos adeptos;

int uid;
static int newID;
date validade;
string dono;
bool vendido;

 public:

Bilhete(date validade, string dono, bool vendido);
Bilhete();

int getID() const;
void setID(int id);

date getValidade() const;
void setValidade(date date);

string imprimeBilhete() const;

//Adepto* getDono() const;
//void setDono (Adepto &a1);

bool getEstado() const;
bool setVendido(Bilhete &b1);
};

【问题讨论】:

  • 哈希表有什么用?你在哈希表中存储了什么?
  • 现在考虑应用程序还应该管理观众,向球队的支持者销售电子门票。购买机票时,这与您的电子邮件地址中的买家相关联;其他数据也必须与门票相关联,如节目说明、支持者姓名和地址。门票信息存储在哈希表中。哈希表应包含与粉丝关联的门票信息。

标签: c++ algorithm data-structures hashtable


【解决方案1】:

我的问题是,关键是什么

我认为关键是票。这样您就可以通过票号获取持票人的信息。

我应该在哪里实现我的哈希函数

我认为这不重要。我可能会创建另一对文件:TicketHash.hppTicketHash.cpp

我仍然不知道如何将门票与粉丝(所有者)电子邮件相关联

哈希函数必须以ticket为参数,返回hash表中cell的个数(或指向cell的指针)以及ticket持有者的相应信息。

我认为你甚至可以像unsigned int hash(Ticket&amp; ticket) { return ticket.number; } 那样制作函数,但它只是一个数组而不是哈希表。

哈希函数的例子见this question

【讨论】:

  • 我编辑了我的问题,我正在尝试类似的方法。对吗?
  • 我没有看到哈希函数(可能我只是想念它)。尝试在谷歌上搜索 c++ 哈希表的示例 - 就像这个 pumpkinprogrammer.com/2014/06/21/… 见函数 int HashTable::hash( string itemKey ) - 这是这个例子中的哈希函数
【解决方案2】:

我会通过以下方式实现它:

Class Tickets{
    String number;
    //Other details...
}

Class Fans{
    ArrayList<Tickets> list;
    String email;
    int index;      //this is an auto increment field which I'd have used in my implementation, just for the ease of further operations. This actually helps.
    //Other details
}

Class Hash{
    //KEY
    String[] key;    //index and email in `Fans` class are unique values
    //Value
    ArrayList<Tickets>[] value;

    //function to assign values
    void assign(String id, Ticket ticket){
        if(key.contains(id))
            value<index of id>.add(Ticket);
        else
            value<new index>.add(Ticket);
    }

    //function that returns value
    Arraylist<Tickets> value(String id){
        return value<index of id>;
    }
}

编辑:

对不起,我没有看到标签 c++。我已经用类似 JAVA 的语法编写了它,但由于它是粗略的逻辑,它应该是可以理解的。如果有任何混淆,请在下面发表评论。您可以在 cpp 中使用 vector&lt;&gt;list&lt;&gt; 代替 ArrayList。

【讨论】:

    猜你喜欢
    • 2012-06-03
    • 2011-02-27
    • 2011-10-14
    • 2011-09-15
    • 2021-09-14
    • 2013-05-31
    • 1970-01-01
    • 2012-04-15
    相关资源
    最近更新 更多