直接用map容器建立对应关系水过…首先别忘记清空整个map容器,最后输入键使对应的值+1就好了。代码如下:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <utility>
#define ll long long
#define ull_ unsigned long long
using namespace std ;
int main(){
int n ;
cin >> n ;
map<int , int> map_ ;
map_.clear() ;
for ( int i = 0 ; i < n ; i ++ ){
int x ;
cin >> x ;
map_[x] ++ ;
}
map<int , int>::iterator it ;
for ( it = map_.begin() ; it != map_.end() ; it ++ ){
cout << it->first << " " << it->second << endl ;
}
return 0 ;
}
本文介绍了一种利用C++中的map容器来建立键值对应关系的方法,通过清空map容器并逐个增加元素的方式,实现了对输入数据的计数统计。文章提供了完整的代码示例,展示了如何遍历map容器并输出键及其对应的值。
707

被折叠的 条评论
为什么被折叠?



