怎么统计string字符串中出现频率最高的三个字符,包括汉字大小写字母

int main
stiring str;
cin>>str;
cout<<“出现频率最高的三个字符是”
cout<<
cout<<“出现频率分别为”
cout<<

这个可以先把String中的字符按字典序排序。然后就是用序列判重的方法就可以得出了。

对啊,遍历字符串啊,遇到重复的加一


#include <map>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef pair<int, int> PAIR;

int cmp(const PAIR &x, const PAIR &y)  
{  
    return x.second > y.second;  
}

int main()
{
    map<int, int> _map;
    string s;
    cin >> s;
    int n = s.length();
    int i;
    for (i = 0; i < n; i++)
    {
        _map[s[i]]++;
    }
    vector<PAIR> vec;
    for (map<int, int>::iterator ltr = _map.begin(); ltr != _map.end(); ltr++)
    {
        vec.push_back(make_pair(ltr->first, ltr->second));
    }
    sort(vec.begin(), vec.end(), cmp);
    vector<PAIR>::iterator curr = vec.begin();
    for (i = 0; i < 3 && curr != vec.end(); i++, curr++)  
    {  
        cout << (char)curr->first << "\t" << curr->second << endl;  
    }
    return 0;
}

helloworld
l 3
o 2
d 1
Press any key to continue