0
我有3個類:二叉搜索樹,字典和聯繫人列表。字典使用二叉樹,現在我想讓聯繫人列表使用字典。問題是,與樹和字典類不同,字典和聯繫人類使用不同數量的參數。這意味着我可以在字典類中使用wiktionary.find(),如代碼中所標記的,我無法對聯繫人列表類中的wontact.find()也做同樣的處理,這也是標記的。任何人都可以幫助我弄清楚如何使用聯繫人列表類中的字典功能?其他類中的類函數 - 二叉樹,詞典,聯繫人列表(C++)
詞典(這個工程,並從樹使用功能)
#include "bst.h"
template <class K, class V> class Dictionary
{
public:
BinarySearchTree<K,V> wiktionary; //this is what I can't do in contact list
Dictionary()
{
}
~Dictionary()
{
}
V & find (K key)
{
wiktionary.find(key); //uses find function in tree
return wiktionary.find(key);
}
V & operator[] (K key)
{
try
{
find(key);
}catch(key_not_found_exception)
{
insert(key, 0);
}
return find (key);
}
private:
};
和聯繫人列表:
#include "dictionary.h"
class ContactList
{
public:
ContactList()
{
}
~ContactList()
{
}
Dictionary<K,V> wontact; //This is what I'm trying to achieve,
//but K and V are invalid according to compiler
/*
* Looks for an entry in contacts that matches name and
* returns the phone number of that entry
* Returns:
* if found string representing contacts phone number
* else returns an empty string
*/
string lookup (string name)
{
wontact.find(name); //this is what I'd like to do
//Work in progress
else
return "";
}
private:
};
有了模板,你的'K'和'V'在'Dictionary'是*佔位符*的時候你使用這個類。假設「關鍵」和「價值」應該是什麼類型......?也許是'std :: string'? – crashmstr 2014-12-03 20:17:51
是的,這兩個都是字符串 – Mock 2014-12-03 20:28:59
然後在'Dictionary wontact;'用'std :: string'替換'K'和'V'(就像你爲'std :: vector '所做的那樣)。 –
crashmstr
2014-12-03 20:30:08