P5266 【深基17.例6】学籍管理

本文详细介绍了一段使用C++ STL中的map数据结构实现的程序,该程序通过三种操作:添加、查找和删除,展示了map在处理键值对数据时的强大功能。程序包括输入输出流程,以及如何利用map进行高效的数据管理和查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

STL天下第一!C++天下第一!

用map不要太爽了~

#include <iostream>
#include <map>
using namespace std;
map<string ,int > t;//存储姓名和分数
typedef map<string ,int >::iterator  It;
    int main()
    {
        int n,x;
        cin>>n;
        string str;
        int score;
        for(int i=1;i<=n;i++)
        {
            cin>>x;

            if(x==1)
            {
                cin>>str;
               cin>>score;
               t.find(str)==t.end() ? t[str]=score :t.find(str)->second=score;
               cout<<"OK"<<endl;
            }
            else if(x==2)
            {
                cin>>str;
                cout<<(t.find(str)==t.end() ? "Not found":to_string(t.find(str)->second))<<endl;
            }
            else if(x==3)
            {
                cin>>str;
               if(t.find(str)==t.end())
               {
                   cout<<"Not found"<<endl;
               }
               else
               {
                   t.erase(str);
                   cout<<"Deleted successfully"<<endl;
               }
            }
            else
            {
                cout<<t.size()<<endl;
            }
        }
        return 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值