Count word frequencies, and print them most-frequent first I'm writing a comparison of a simple word-counting program in various languages. The task is to count the frequencies of unique, space-separated words in the input, and output the results most frequent first. Case should be normalized to lowercase (ASCII is okay).
But it's been a long time since I've written C++ (before the C++11 days). Is this idiomatic modern C++? Any improvements I could make to make this more idiomatic or simpler (I'm not looking for efficiency improvements here). I want to stick to the standard library.
#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;unordered_map&gt;
#include &lt;vector&gt;

using namespace std;

int main() {
    string word;
    unordered_map&lt;string, int&gt; counts;
    while (cin &gt;&gt; word) {
        transform(word.begin(), word.end(), word.begin(),
            [](unsigned char c){ return tolower(c); });
        counts[word]++;
    }

    vector&lt;pair&lt;string, int&gt;&gt; ordered(counts.begin(), counts.end());
    sort(ordered.begin(), ordered.end(), [](auto &amp;a, auto &amp;b) {
        return a.second &gt; b.second;
    });

    for (auto count : ordered) {
        cout &lt;&lt; count.first &lt;&lt; &quot; &quot; &lt;&lt; count.second &lt;&lt; &quot;\n&quot;;
    }
    return 0;
}
Mainly this looks fine.

using namespace std; is a bad habit to get into due to potential name collisions. Instead prefer to type std:: where necessary.

counts[word]++;. We don't need an un-incremented copy of the count, so we should use pre-increment here instead: ++counts[word];

sort(ordered.begin(), ordered.end(), [](auto &amp;a, auto &amp;b) we don't alter a or b in the lambda, so these should be const&amp;.

for (auto count : ordered) copies the pairs. Again, we should be using auto const&amp; here.

return 0; we don't need this, as the compiler will add it for us automatically. We might also use a named constant instead of 0, specifically EXIT_SUCCESS from &lt;cstdlib&gt;.