fork(1) download
  1. #include <iostream>
  2. #include <map>
  3. #include <functional>
  4. #include <utility>
  5. #include <cctype>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. map<string,size_t> words;
  12. string word;
  13.  
  14. while (cin>>word)
  15. {
  16. for(char&c:word)c=tolower(c);
  17. ++words[word];
  18. }
  19. cout<<" ----- By word: ------" ;
  20. for(const auto& w : words) cout<<endl<<w.first<<":"<<w.second;
  21. cout<<endl<<endl<<" ----- By frequency: ------";
  22. multimap<size_t,string,greater<int>> byFreq;
  23. for(const auto& w : words) byFreq.insert( make_pair(w.second, w.first) );
  24. for(const auto& w : byFreq) cout<<endl<<w.second <<":"<<w.first;
  25. return 0;
  26. }
Success #stdin #stdout 0s 2992KB
stdin
This is some simple base text to use for comparison with other files.
You may use your own if you so choose; your program shouldn't actually care.
For getting interesting results, longer passages of text may be useful.
In theory, a full novel might work, although it will likely be somewhat slow.
This is some simple base text to use for comparison with other files.
This is some simple base text to use for comparison with other files.
stdout
  ----- By word: ------
a:1
actually:1
although:1
base:3
be:2
care.:1
choose;:1
comparison:3
files.:3
for:4
full:1
getting:1
if:1
in:1
interesting:1
is:3
it:1
likely:1
longer:1
may:2
might:1
novel:1
of:1
other:3
own:1
passages:1
program:1
results,:1
shouldn't:1
simple:3
slow.:1
so:1
some:3
somewhat:1
text:4
theory,:1
this:3
to:3
use:4
useful.:1
will:1
with:3
work,:1
you:2
your:2

 ----- By frequency: ------
for:4
text:4
use:4
base:3
comparison:3
files.:3
is:3
other:3
simple:3
some:3
this:3
to:3
with:3
be:2
may:2
you:2
your:2
a:1
actually:1
although:1
care.:1
choose;:1
full:1
getting:1
if:1
in:1
interesting:1
it:1
likely:1
longer:1
might:1
novel:1
of:1
own:1
passages:1
program:1
results,:1
shouldn't:1
slow.:1
so:1
somewhat:1
theory,:1
useful.:1
will:1
work,:1