map 随笔

map是个非常好用的STL
小面会演示map的几个函数

一、赋值与查询

#include <bits/stdc++.h>
using namespace std;
map<int,int>mp; 
int main(){
	//赋值
	mp[1]=1;
	mp[2]=4;
	mp[3]=9;
	//查询
	printf("%d",mp[2]);
	return 0;
}

二、查询是否存在

#include <bits/stdc++.h>
using namespace std;
map<int,int>mp; 
int main(){
	mp[1]=1;
	mp[2]=4;
	mp[3]=9;
	// 存在返回1、不存在返回0
	printf("%d %d",mp.count(2),mp.count(4));
	return 0;
}

三·、删除元素

#include <bits/stdc++.h>
using namespace std;
map<int,int>mp; 
int main(){
	mp[1]=1;
	mp[2]=4;
	mp[3]=9;
	printf("%d\n",mp.count(2));
	mp.erase(2);
	printf("%d",mp.count(2));
	return 0;
}
posted @   KevinLikesCoding  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
历史上的今天:
2021-10-05 python class自身返回值研究
点击右上角即可分享
微信分享提示