-2
値に基づいて出力をソートしようとしていますが、そのアプローチ方法がわかりません。 これは私の現在の出力です:C++のマップで値をソートする方法
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";
}
}
書くことができ、この場合-else文の代わりに、あなたのプログラムの中でそれに注意を払っています私はあなたが誰に投票したのか初心者の興味深い質問。 –
あなたはstd :: unordered_map –