写一个通用类字典dictionary,每个词条包含一组键和值,它们可以是任何类型数据,包括自定义数据类型
https://zhidao.baidu.com/question/179007805894887724.html?fr=push具体问题看看这个...
https://zhidao.baidu.com/question/179007805894887724.html?fr=push 具体问题看看这个
展开
1个回答
展开全部
来举个栗子, 不然不清楚具体需要的是什么
#include<iostream>
#include <string>
#include <map>
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<string,CItem> 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<string,CItem>::iterator it = m_AllWord.begin();it != m_AllWord.end();++it)
{
cout<<"Word:"<<it->second.Eng()<<endl;
cout<<"Parap:"<<it->second.Ch()<<endl;
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");
}
是这意思吗?
#include<iostream>
#include <string>
#include <map>
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<string,CItem> 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<string,CItem>::iterator it = m_AllWord.begin();it != m_AllWord.end();++it)
{
cout<<"Word:"<<it->second.Eng()<<endl;
cout<<"Parap:"<<it->second.Ch()<<endl;
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");
}
是这意思吗?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询