Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. using std::cin;
  4. using std::cout;
  5. using std::endl;
  6. #include <string>
  7. using std::string;
  8. #include <map>  // also known as an "associative array"
  9. using std::map; // you can think of maps kind of like arrays,
  10.                 // but the indexes no longer have to be integers...
  11.                 // e.g., we could do A["a string"] = 100;
  12.  
  13. int main() {
  14.     map<string,int> F;
  15.     string s; /* hold input from stdin */
  16.     while (cin >> s) F[s]++;
  17.     /* that's all it takes to get frequency data. */
  18.     /* how to go through a map???  Use an iterator: */
  19.     for (map<string,int>::iterator i = F.begin(); i!=F.end(); i++)
  20.         cout << (*i).first << " : " << (*i).second << endl;
  21.     /*             ^^^^^^ "key"          ^^^^^ "value" */
  22.     return 0;
  23.     /* TODO: try to do this without using maps. */
  24.     /* TODO: make an actual histogram, instead of just a freq. table. */
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement