您的位置:首页 > 其它

根据hash统计数组中各个元素的数量

2017-09-02 21:57 190 查看
#include<iostream>
#include<map>
#include<algorithm>
#include<string>
#include<vector>
#include<sstream>
#include<stdio.h>
using namespace std;
void main()
{
map<int, int> a;
vector<int> b;
int temp;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> temp;
b.push_back(temp);
}

for (int i = 0; i < b.size(); i++)
{
if (a.count(b.at(i)) == 0)
a[b.at(i)] = 1;
else
{
a[b.at(i)] = a[b.at(i)]+1;
}
}
map<int, int>::iterator it;
for (it = a.begin(); it != a.end(); it++)
{
cout << it->first << "  "<<it->second << endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: