飞越的心


私信TA

用户名:H2030819089

访问量:17339

签 名:

等  级
排  名 75
经  验 9578
参赛次数 18
文章发表 38
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

TA的其他文章

解题思路:

map

其中一个是键的类型

第二个是值得类型

first或second用法,这是因为map中的每个元素都对应一组

键值对中的第一个成员称为first,第二个成员称为second.

无标题.png


注意事项:

如果是int型映射到int型,就相当于是普通的int型数组

但是如果是字符串到整型的映射,必须是string而不是char数组

map

//这里的iterator是在template class map中声明的一个类,所以需要用map

map



参考代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,x;
    map<int,int>mp1;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
         cin>>x;
         mp1[x]++;
    }
    map<int,int>::iterator it;
    for(it=mp1.begin();it!=mp1.end();it++)
    {
         cout<<it->first<<" "<<it->second<<endl;
    }
}
 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区