fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. map<string, int> wordCount;
  11.  
  12. vector<string> inputWords = {"some", "test", "stuff", "test",
  13. "stuff"}; //read from file instead
  14.  
  15. for(auto& s: inputWords)
  16. wordCount[s]++; //wordCount itself
  17.  
  18.  
  19. for(auto& entry: wordCount) //print all words and assosiated counts
  20. cout << entry.first << " " << entry.second <<endl;
  21.  
  22.  
  23. cout <<wordCount.size() <<endl; //thats number of distinct words
  24. }
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
some 1
stuff 2
test 2
3