入门STL(map/multiset)

目录

​编辑

1.map

输入

输出

样例

输入

输出

解题代码:

2.multiset

输入

输出

样例

输入

输出

解题代码:

留下你的足迹吧!谢谢。


1.map

map函数是一个内置函数,它允许你对一个序列)的每个元素应用一个函数,并收集结果作为一个新的序列返回。这是函数式编程范式的一个常用工具,可以用来替代某些循环操作,使代码更加简洁和可读。

例题:

给定n个数字,统计出现次数最多的。如果有多个数字出现次数都是最多的,从小到大输出。

输入

第一行一个整数n,表示数字个数。

接下来一行n个数。

输出

从小到大输出,次数最多的数字。

样例

输入

复制代码
10
1 1 2 3 2 2 2 3 3 3

输出

复制代码
2 3
cpp 复制代码
map<int,int>::iterator

这是map的基本语法,用它,我们就可以写出遍历循环:

cpp 复制代码
for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)
{

}
	

map可以理解为两个二维数组,而it(iterable) 便是指针:

cpp 复制代码
it->first
cpp 复制代码
it->second

由此可得到

解题代码:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
map<int,int>mp;
int main(){
	int n,x,mx=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>x;
		mp[x]++;
		if(mp[x]>mx)mx=mp[x];
	}
	for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++){
		if(it->second==mx){
			printf("%d ",it->first);
		}
	}
	return 0;
} 

2.multiset

C++中multiset容器是STL模板<set>库中一个非常有用的类型,它可以看成一个序列,插入一个数,删除一个数都能够在O(logn)的时间内完成,它能时刻保证序列中的数是有序的 ,序列中可以存在重复的数(而set容器要求两两不同,且不保证有序)。

例题:

插入一个数x

删除一个数x,如果不存在,就忽略这条指令。如果有多个,只删除一个。

输出大于x的最小的数,如果没有输出"max"

输出小于x的最大的数,如果没有输出"min"

输出最大值并删除,如果没有元素,忽略这条指令

输出最小值并删除,如果没有元素,忽略这条指令

所有指令结束后,从小到大输出数据结构中所有的数。

输入

第一行一个整数n,表示操作数

接下来n行,每行如下6个之一:

1 x:插入一个数x

2 x:删除x,如果x不存在,忽略这条指令

3 x:输出大于x的最小值,如果没有,输出max

4 x:输出小于x的最大值,如果没有,输出min

5 :输出最大值,并删除,如果没有,忽略这条指令

6:输出最小值,并删除,如果没有,忽略这条指令

输出

根据每条指令,输出相应的结果。

样例

输入

复制代码
10
1 3
1 5
1 6
3 4
4 5
1 10
2 5
5
6
1 8

输出

复制代码
5
3
10
3
6 8

插入一个数:

cpp 复制代码
scanf("%d",&f);//输入
st.insert(f);//插入

找到第一个大于f的数:

cpp 复制代码
it=st.lower_bound(f);//查找

找到第一个小于f的数:

cpp 复制代码
it=st.upper_bound(f);//查找

mulitiset容器中的第一个数:

cpp 复制代码
st.begin()

mulitiset容器不为空:

cpp 复制代码
!st.empty()

由此可得到

解题代码:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
multiset<int> st;
int main(){
	int n;
	scanf("%d",&n);
	int x,f; 
	multiset<int>::iterator it;
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		if(x==1){
			scanf("%d",&f);
			st.insert(f);
		}
		else if(x==2){
			scanf("%d",&f);
			it=st.lower_bound(f);
			if(it!=st.end()&&*it==f){
				st.erase(it);
			} 
		}
		else if(x==3){
			scanf("%d",&f);
			it=st.upper_bound(f);
			if(it!=st.end()&&!st.empty()){
				printf("%d\n",*it);
			}
			else{
				printf("max\n");
			}
		}
		else if(x==4){
			scanf("%d",&f);
			it=st.lower_bound(f);
			if(it!=st.begin()&&!st.empty()){
				it--;
				printf("%d\n",*it);
			}
			else{
				printf("min\n");
			}
		}
		else if(x==5){
			multiset<int>::iterator it1 = st.end();
			if(st.empty())continue;
			it1--;
			int r=*it1;
			printf("%d\n",r);
			st.erase(it1);
		}
		else{
			if(st.empty())continue;
			int r=*st.begin();
			printf("%d\n",r);
			st.erase(st.begin());
		}
	}	 
	for(it=st.begin();it!=st.end();it++){
		printf("%d ",*it);
	}
	return 0;		
}

留下你的足迹吧!谢谢。

相关推荐
XiaoyuEr_66885 分钟前
如何创建一个C#项目(基于VS2022版)
开发语言·c#
什么半岛铁盒7 分钟前
Linux线程与进程:探秘共享地址空间的并发实现与内
linux·c++
AI_RSER19 分钟前
基于 Google Earth Engine 的南京江宁区土地利用分类(K-Means 聚类)
算法·机器学习·分类·kmeans·聚类·遥感·gee
智践行25 分钟前
ROS2 Jazzy:创建自定义的消息和服务接口(C++)
c++·操作系统
Mercury-circle30 分钟前
JavaScript基础知识合集笔记1——数据类型
开发语言·javascript·笔记
Small踢倒coffee_氕氘氚36 分钟前
是否应该禁止危险运动论文
经验分享·笔记·算法·灌灌灌灌
Chase_______39 分钟前
Java后端开发——分层解耦详解
java·开发语言·spring·web
喝可乐的布偶猫40 分钟前
Java----super 关键字
java·开发语言
篱笆院的狗1 小时前
Java 中 ConcurrentHashMap 1.7 和 1.8 之间有哪些区别?
java·开发语言
Maple_land1 小时前
C++初阶——string的模拟实现(上)
c++