我正在尝试根据值对输出进行排序,我不确定如何处理它。 这是我目前的输出:
E:2
H:1
I:3
L:2
N:3
O:2
S:2
T:1
Y:1
这就是我想要输出的方式:
I: 3
N: 3
E: 2
L: 2
O: 2
S: 2
H: 1
T: 1
Y: 1
我的代码:
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<string>
using std::string;
#include<map>
using std::map;
#include<algorithm>
using std::sort;
int main()
{
string input;
int line = 0;
map<char, int> letters;
while (getline(cin, input))
{
line += 1;
for (int i = 0; i < input.length(); i++)
{
if (isalpha(input[i]))
{
if (letters.count(toupper(input[i])) == 0)
{
letters[toupper(input[i])] = 1;
}
else
{
letters[toupper(input[i])] += 1;
}
}
}
}
cout << "Processed " << line << " line(s)." << endl;
cout << "Letters and their frequency:" << endl;
for (auto it = letters.cbegin(); it != letters.cend(); ++it)
{
cout << it->first << ":" << it->second << "\n";
}
}
答案 0 :(得分:1)