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

"这段代码展示了如何利用C++标准库中的unordered_map容器进行增删查改操作。程序读取整数n,根据输入的a值(1-添加,2-查询,3-删除,4-查询大小)执行对应操作。对于添加和删除,它会更新map,并输出相应的操作结果。对于查询操作,如果找到键值对则输出对应的值,否则输出"Not found"。最后,当输入为4时,输出unordered_map的大小。"
摘要由CSDN通过智能技术生成

题目链接:https://www.luogu.com.cn/problem/P5266


查找与删除,选用STL容器中的unordered_map

#include <iostream> 
#include <cstdio> 
#include <cstring>
#include <algorithm>
#include <unordered_map>

using namespace std;

int n;
unordered_map<string, int> h;

int main(){
	cin >> n;
	while (n -- ){
		int a; cin >> a;
		if (a == 1) {
			string b; int c; cin >> b >> c;
			h[b] = c;
			puts("OK");
		}
		else if (a == 2){
			string b; cin >> b;
			if (h.count(b)) cout << h[b] << endl;
			else puts("Not found");
		}
		else if (a == 3){
			string b; cin >> b;
			if (h.count(b)) h.erase(b), puts("Deleted successfully");
			else puts("Not found");
		}
		else {
			cout << h.size() << endl;
		}
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值