#include <iostream>
#include <string>
#include <map>
using namespace std;
struct Worker
{
string name;
int wage;
Worker(){}
~Worker(){}
void setname()
{
cin>>name;
}
void setwage()
{
cin>>wage;
}
string getname()
{
return name;
}
int getwage()
{
return wage;
}
};
int main()
{
map<string,int> m;
map<string,int>::iterator im;
int i(0);
cout<<"请输入工人信息:"<<endl;
while(i!=2)
{
Worker ww;
cout<<"姓名:";
ww.setname();
cout<<"工资:";
ww.setwage();
m.insert(make_pair(ww.getname(),ww.getwage()));
//存入信息
i++;
}
cout<<endl<<"姓名"<<"工资"<<endl;
for(im=m.begin();im!=m.end();im++)
cout<<(*im).first<<":"<<(*im).second<<endl;
string wfind;
cin.seekg(ios::cur);
cout<<"请输入要查询的工人的名字:";
getline(cin,wfind);
if(m[wfind] ==0)
{
cout<<"输入名字有错,不存在该工人!";
exit(0);
}
cout<<"工人"<<wfind<<"的工资为:"<<m[wfind]<<endl;
return 0;
}
没有用到指针
代码还可以优化和扩展