首页 > 编程语言 > 详细

c++ map的使用

时间:2019-10-23 23:57:44      阅读:111      评论:0      收藏:0      [点我收藏+]

题目描述:

n(n<=200000)个数(1.5*10^9范围内),输出重复的数(最多10000个)出现的次数

代码:

#include <iostream>
#include <map>
#include <cstdio>

using namespace std;
map<int,int> a;
int n,t;

int main()
{
	freopen("count.in","r",stdin); //打开输入文件
	freopen("count.out","w",stdout); //打开输出文件
	
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>t;
		a[t]++;	
	} 
	for(map<int,int>::iterator it=a.begin();it!=a.end();it++)
		cout<<it->first<<" "<<it->second<<" "<<endl;
		
	fclose(stdin);//关闭输入文件
	fclose(stdout);//关闭输出文件
} 

几个操作总结:

1.定义map<数据类型,数据类型> 变量名;

2.首元素:x.began()

3.尾元素:x.end()

4.遍历:

先定义一个迭代器:

map<数据类型,数据类型>::iterater it;

接着:

it=a.begin();it!=a.end();it++

这里it相当于指针.

4.输出元素:
定义的第一个:it->first

第二个:it->second

c++ map的使用

原文:https://www.cnblogs.com/huaruoji/p/11729426.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!