MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / showWordCounts

Function showWordCounts

Examples/Modules/Chapter 20/Ex20_05/Ex20_05.cpp:72–95  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

70}
71
72void 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}

Callers 1

mainFunction · 0.70

Calls 1

maxWordLengthFunction · 0.70

Tested by

no test coverage detected