#include <unordered_map>
#include <iostream>
#include <set>

int main() {
	std::string input = "hello world";

    std::unordered_map<char, unsigned int> count;
    for (char character : input)
        if (character >= 'a' && character <= 'z')
            count[character]++;

    std::cout << "Unsorted list:" << std::endl;
    for (auto const &kv : count)
        std::cout << kv.first << " = " << kv.second << std::endl;

    using myPair = std::pair<char, unsigned int>;
    auto comp = [](const myPair& a, const myPair& b) {
        return (a.second > b.second || a.second == b.second && a.first < b.first);
    };
    std::set<myPair, decltype(comp)> sorted(comp);
    for(auto const &kv : count)
        sorted.insert(kv);

    std::cout << "Sorted list according to frequency then alphabetically:" << std::endl;
    for (auto const &kv : sorted)
        std::cout << kv.first << " = " << kv.second << std::endl;

	return 0;
}