#include #include #include #include using namespace std; typedef /* table mapping strings to unsigned int */ Table; struct WordCounts { string word; unsigned count; WordCounts(): count(0) {} WordCounts (string w, unsigned c) : word(w), count(c) {} bool operator< (const WordCounts& wc) { if (count > wc.count) return true; else if (count == wc.count) return (word < wc.word); else return false; } }; typedef list Concordance; int main () { const char UpperToLower = 'a' - 'A'; Table words; while (cin) { string w; char c; do { cin.get(c); if ((c >= 'A') && (c <= 'Z')) c += UpperToLower; if ((c >= 'a') && (c <= 'z')) w += c; } while ((cin) && ((c >= 'a') && (c <= 'z'))); if (w.size() > 0) { if (/* w is not in words */) /* put [w,0] into words */ /* increment count associated with w in words */ ; } } Concordance concord; for (/* all [w,c] pairs in words */) /* add WordCounts(w,c) to concord; */ // sort concord into descending order by count; concord.sort (); for (/* all WordCounts wc in concord */) { /* ... */ cout << wc.word << ' ' << wc.count << endl; } return 0; }