2015-12-18 127 views
1

我有这个项目,我需要实现一个哈希表。我有两个班:粉丝和门票。球迷可以购买门票,每张门票都与球迷的电子邮件相关联。哈希表 - 散列函数实现

我的问题是,什么是关键,我应该在哪里实现我的散列函数?我的猜测是它会在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); 
}; 
+1

什么是哈希表?你在散列表中存储什么? – Baldrick

+0

现在考虑应用程序还应该管理观众,向团队支持者销售电子票。购买机票时,这与您的电子邮件地址中的买方相关联;其他数据也必须与票证相关联,如展示导致,支持者姓名和地址。 门票的信息存储在散列表中。散列表应包含与粉丝相关的门票信息。 – Perseverance

回答

0

我的问题是,什么是关键

我认为关键是票。所以你可以通过门票号码获得持票人的信息。

,并在那里我要实现我的散列函数

我不认为事情。我可能会创建另一个文件对:TicketHash.hppTicketHash.cpp

我仍然不知道我怎么我会联想把车票给风扇(所有者)电子邮件

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

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

对于哈希函数的例子见this question

+0

我编辑了我的问题,我在尝试类似的东西。这是对的吗? – Perseverance

+0

我没有看到哈希函数(可能我只是想念它)。尝试google C++哈希表的例子 - 像这样http://pumpkinprogrammer.com/2014/06/21/c-tutorial-intro-to-hash-tables/ 查看函数int HashTable :: hash(string itemKey )' - 这是这个例子中的散列函数 –

0

我会以下列方式已经实现了这一点:

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语法编写它,但由于它是粗糙的逻辑,它应该是可以理解的。如果有任何混淆,请在下面发表评论。代替ArrayList,您可以在cpp中使用vector<>list<>