关联式容器映射map练习

//管理学生名册

‍#include <iostream>
#include <map>
#include <string>
using namespace std;
struct student
{
string id;
string name;
int grade;

};

int main()
{
map<string,student> stubook;
student stu;
cout<<"--creat student book--"<<endl;
for(int i=0;i<2;i++)
{
   cin>>stu.id;
   cin>>stu.name;
   cin>>stu.grade;
   stubook[stu.id]=stu;
}

//判断student book是否为空
if(stubook.empty()) cout<<"stubook is empty!"<<endl;
else cout<<"stubook isnot empty!"<<stubook.size()<<endl;

//输出student book
cout<<"---print student book--"<<endl;

map<string,student>::iterator iter=stubook.begin();

for( ;iter!=stubook.end();iter++)
{
cout<<"student book:"<<iter->second.id<<" - "<<iter->second.name<<" -"<<iter->second.grade<<endl;
}

//查找学生
cout<<"---find a student(please input studentid)--"<<endl;
string studentid;
cin>>studentid;
iter=stubook.find(studentid);
if(iter==stubook.end())
{
   cout<<"cannot find: "<<studentid<<endl;
}
else
{
   cout<<"student: "<<iter->second.id<<" - "<<iter->second.name<<" - "<<iter->second.grade<<endl;

   cout<<"do you want to delete the student (y/n):"<<endl;
   char c;
   cin>>c;
   if(c=='y')
   {
     stubook.erase(studentid);

   }
      

}

//输出student book
cout<<"---print student book--"<<endl;
map<string,student>::iterator iter2=stubook.begin();

for( ;iter2!=stubook.end();iter2++)
{
cout<<"student book:"<<iter2->second.id<<" - "<<iter2->second.name<<" -"<<iter2->second.grade<<endl;
}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值