I am trying to sort my output based on the value and I am unsure on how to approach it. This is my current output:
 E:2  
 H:1 
 I:3 
 L:2 
 N:3 
 O:2 
 S:2
 T:1 
 Y:1 
This is how I want my output:
 I: 3 
 N: 3
 E: 2 
 L: 2 
 O: 2 
 S: 2 
 H: 1 
 T: 1 
 Y: 1
My code:
#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";
    }
}
Aucun commentaire:
Enregistrer un commentaire