#include <iostream>
#include <map>
using namespace std;
int main()
{
int array[11] = {1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
map<int, int, less<int>> hash;
//less表示该哈希表中先存放小数字的次数,再存放大数字的次数。在此处就是,先存放数组中1出现的次数,在存放数组中2出现的次数……
//反之则greater
for(int i = 0; i < 11; i++)
hash[array[i]]++;
//显示hash中的内容
map<int, int>::iterator it = hash.begin();
while(it != hash.end())
{
cout << "数组array中" << it->first << "出现的次数是" << it->second << endl;
it++;
}
return 0;
}
结果输出为,
数组array中1出现的次数是1
数组array中2出现的次数是1
数组array中3出现的次数是3
数组array中4出现的次数是2
数组array中5出现的次数是4