2015-12-18 186 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<>