fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <map>
  5. using namespace std;
  6.  
  7. int main() {
  8.  
  9. ifstream file; // A in file stream, the c++ way of reading files.
  10. //file.open ("example.txt");
  11. string word; // A word to store each word.
  12. std::map<std::string, int> myWords;
  13.  
  14. // Read each word.
  15. while ( cin >> word) {
  16. if (myWords.find(word) == myWords.end()) {
  17. myWords[word] = 1;
  18. } else {
  19. myWords[word]++;
  20. }
  21. }
  22.  
  23. for (auto wordPair : myWords) {
  24. std::cout << wordPair.first << " " << wordPair.second << std::endl;
  25. }
  26.  
  27. // your code goes here
  28. return 0;
  29. }
Success #stdin #stdout 0s 3420KB
stdin
Class
Text
Class
fall
mark
mark
Text
stdout
Class 2
Text 2
fall 1
mark 2