| 70 | } |
| 71 | |
| 72 | void showWordCounts(const WordCounts& wordCounts) |
| 73 | { |
| 74 | const size_t field_width{maxWordLength(wordCounts) + 1}; |
| 75 | const size_t words_per_line{5}; |
| 76 | |
| 77 | size_t words_in_line{}; // Number of words in the current line |
| 78 | char previous_initial{}; |
| 79 | for (const auto& [word, count] : wordCounts) |
| 80 | { |
| 81 | if (count < 2) continue; // Skip words that appear only once |
| 82 | |
| 83 | // Output newline when initial letter changes or after 5 per line |
| 84 | if ( (previous_initial && word[0] != previous_initial) |
| 85 | || words_in_line++ == words_per_line) |
| 86 | { |
| 87 | words_in_line = 0; |
| 88 | std::cout << std::endl; |
| 89 | } |
| 90 | // Output "word (count)", where word has a dynamic field width |
| 91 | std::cout << std::format("{:>{}} ({:2})", word, field_width, count); |
| 92 | previous_initial = word[0]; |
| 93 | } |
| 94 | std::cout << std::endl; |
| 95 | } |