fork download
  1. #include <cstdlib>
  2. #include <cctype>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <string>
  7. #include <unordered_map>
  8.  
  9. // turn string to lower case
  10. std::string make_lower(std::string s)
  11. {
  12. std::transform(s.begin(), s.end(), s.begin(), ::tolower);
  13. return s;
  14. }
  15.  
  16. // custom sort comparator. sorts on larger numbers, then alphabetic on strings
  17. struct cmp_custom
  18. {
  19. bool operator()(const std::pair<unsigned int, std::string>& lhs,
  20. const std::pair<unsigned int, std::string>& rhs) const
  21. {
  22. return (rhs.first < lhs.first) ||
  23. (!(lhs.first < rhs.first) && (lhs.second < rhs.second));
  24. }
  25. };
  26.  
  27. int main(int argc, char *argv[])
  28. {
  29. // load test words file (we use stdin for this website)
  30. std::unordered_map<std::string, unsigned int> words;
  31. std::string str;
  32. while (std::cin >> str)
  33. ++words[make_lower(str)];
  34.  
  35. // build a vector of reversed pairs (count first, word second)
  36. std::vector<std::pair<unsigned int, std::string>> pairs;
  37. std::transform(words.begin(), words.end(), std::back_inserter(pairs),
  38. [](const std::pair<std::string,unsigned int>& obj)
  39. {
  40. return std::make_pair(obj.second,obj.first);
  41. });
  42.  
  43. // sort using custom comparator
  44. std::sort(pairs.begin(), pairs.end(), cmp_custom());
  45.  
  46. for (auto const& rec : pairs)
  47. std::cout << rec.second << " (" << rec.first << ")\n";
  48.  
  49. return EXIT_SUCCESS;
  50. }
Success #stdin #stdout 0s 3440KB
stdin
All the worlds a stage And all the men and women merely players They have their exits and their entrances And one man in his time plays many parts His acts being seven ages At first the infant Mewling and puking in the nurses arms Then the whining schoolboy with his satchel And shining morning face creeping like snail Unwillingly to school And then the lover Sighing like furnace with a woeful ballad Made to his mistress eyebrow Then a soldier Full of strange oaths and bearded like the pard Jealous in honor sudden and quick in quarrel Seeking the bubble reputation Even in the cannons mouth And then the justice In fair round belly with good capon lined With eyes severe and beard of formal cut Full of wise saws and modern instances And so he plays his part The sixth age shifts Into the lean and slippered pantaloon With spectacles on nose and pouch on side His youthful hose well saved a world too wide For his shrunk shank and his big manly voice Turning again toward childish treble pipes And whistles in his sound Last scene of all That ends this strange eventful history Is second childishness and mere oblivion Sans teeth sans eyes sans taste sans everything
stdout
and (18)
the (12)
his (9)
in (7)
with (5)
a (4)
of (4)
sans (4)
then (4)
all (3)
like (3)
eyes (2)
full (2)
on (2)
plays (2)
strange (2)
their (2)
to (2)
acts (1)
again (1)
age (1)
ages (1)
arms (1)
at (1)
ballad (1)
beard (1)
bearded (1)
being (1)
belly (1)
big (1)
bubble (1)
cannons (1)
capon (1)
childish (1)
childishness (1)
creeping (1)
cut (1)
ends (1)
entrances (1)
even (1)
eventful (1)
everything (1)
exits (1)
eyebrow (1)
face (1)
fair (1)
first (1)
for (1)
formal (1)
furnace (1)
good (1)
have (1)
he (1)
history (1)
honor (1)
hose (1)
infant (1)
instances (1)
into (1)
is (1)
jealous (1)
justice (1)
last (1)
lean (1)
lined (1)
lover (1)
made (1)
man (1)
manly (1)
many (1)
men (1)
mere (1)
merely (1)
mewling (1)
mistress (1)
modern (1)
morning (1)
mouth (1)
nose (1)
nurses (1)
oaths (1)
oblivion (1)
one (1)
pantaloon (1)
pard (1)
part (1)
parts (1)
pipes (1)
players (1)
pouch (1)
puking (1)
quarrel (1)
quick (1)
reputation (1)
round (1)
satchel (1)
saved (1)
saws (1)
scene (1)
school (1)
schoolboy (1)
second (1)
seeking (1)
seven (1)
severe (1)
shank (1)
shifts (1)
shining (1)
shrunk (1)
side (1)
sighing (1)
sixth (1)
slippered (1)
snail (1)
so (1)
soldier (1)
sound (1)
spectacles (1)
stage (1)
sudden (1)
taste (1)
teeth (1)
that (1)
they (1)
this (1)
time (1)
too (1)
toward (1)
treble (1)
turning (1)
unwillingly (1)
voice (1)
well (1)
whining (1)
whistles (1)
wide (1)
wise (1)
woeful (1)
women (1)
world (1)
worlds (1)
youthful (1)