首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >哈希表-哈希函数实现

哈希表-哈希函数实现
EN

Stack Overflow用户
提问于 2015-12-18 21:21:40
回答 2查看 386关注 0票数 1

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

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

我不认为任何代码是必要的,但我会张贴一些,如果有任何疑问。

诚挚的问候

Fan类("Adepto")

代码语言:javascript
复制
class Adepto {

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

公共:

代码语言:javascript
复制
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)

代码语言:javascript
复制
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);
};
EN

回答 2

Stack Overflow用户

发布于 2015-12-18 22:23:08

我的问题是,关键是什么

我认为关键是票证。所以你可以通过票号来获取持票者的信息。

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

我不认为这有什么关系。我可能会创建另一对文件:TicketHash.hppTicketHash.cpp

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

哈希函数必须以票证为参数,返回哈希表中单元格的编号(或指向单元格的指针)以及票证持有者的相应信息。

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

有关散列函数的示例,请参见this question

票数 0
EN

Stack Overflow用户

发布于 2015-12-18 22:37:43

我将通过以下方式实现此功能:

代码语言:javascript
复制
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<>list<>来代替ArrayList。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34356702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档