入门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;		
}

留下你的足迹吧!谢谢。

相关推荐
草莓熊Lotso1 小时前
C++11 核心特性实战:列表初始化 + 右值引用与移动语义(附完整代码)
java·服务器·开发语言·汇编·c++·人工智能·经验分享
初夏睡觉2 小时前
从0开始c++,但是重置版,第1篇(c++基本框架)
开发语言·c++
前端小白在前进2 小时前
⭐力扣刷题:螺旋矩阵
算法·leetcode·矩阵
草莓熊Lotso2 小时前
GCC/G++ 编译器完全指南:从编译流程到进阶用法(附实操案例)
linux·运维·服务器·网络·c++·人工智能·自动化
老赵聊算法、大模型备案7 小时前
北京市生成式人工智能服务已备案信息公告(2025年12月11日)
人工智能·算法·安全·aigc
workflower7 小时前
时序数据获取事件
开发语言·人工智能·python·深度学习·机器学习·结对编程
CoderYanger8 小时前
C.滑动窗口-求子数组个数-越长越合法——2799. 统计完全子数组的数目
java·c语言·开发语言·数据结构·算法·leetcode·职场和发展
C++业余爱好者8 小时前
Java 提供了8种基本数据类型及封装类型介绍
java·开发语言·python
厕所博士8 小时前
红黑树原理前置理解—— 2-3 树
算法·2-3树·红黑树原理理解前置
林杜雨都8 小时前
Action和Func
开发语言·c#