写一个通用类字典dictionary,每个词条包含一组键和值,它们可以是任何类型数据,包括自定义数据类型

2024-12-02 05:30:48
推荐回答(1个)
回答1:

来举个栗子, 不然不清楚具体需要的是什么
#include
#include
#include
using namespace std;
class CItem
{
public:
CItem(string& eng,string& ch);
CItem(){m_EngWord = ""; m_ChPra = "";}
string Eng(){return m_EngWord;}
string Ch() {return m_ChPra;}
private:
string m_EngWord;
string m_ChPra;
};
CItem::CItem(string& eng,string& ch)
:
m_EngWord(eng),
m_ChPra(ch)
{
}

class CDictionary
{
public:
CItem FindItem(string& word);
bool AddItem(string& eng,string& ch);
// bool Save2File(string& file);
void PrintAll();
private:
map m_AllWord;
};

CItem CDictionary::FindItem(string& word)
{
if(m_AllWord.count(word) > 0)
return m_AllWord[word];
else
{
CItem temp;
return temp;
}
}
bool CDictionary::AddItem(string& eng,string& ch)
{
if(eng != "")
{
CItem temp(eng,ch);
m_AllWord[eng] = temp;
return true;
}
return false;
}
// bool CDictionary::Save2File(string& file)
// {
// return true;
// }
void CDictionary::PrintAll()
{
for(map::iterator it = m_AllWord.begin();it != m_AllWord.end();++it)
{
cout<<"Word:"<second.Eng()<cout<<"Parap:"<second.Ch()<cout<<"\n";
}
}

int main()
{
CDictionary dic;
string eng = "zhazha";
string ch = "渣渣";
bool re = dic.AddItem(eng,ch);
CItem te = dic.FindItem(eng);
te = dic.FindItem(ch);
dic.PrintAll();
system("pause");
}
是这意思吗?