home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / MultiSetWordCount.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  966 b   |  34 lines

  1. //: C20:MultiSetWordCount.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. //{L} StreamTokenizer
  7. // Count occurrences of words using a multiset
  8. #include "StreamTokenizer.h"
  9. #include "../require.h"
  10. #include <string>
  11. #include <set>
  12. #include <fstream>
  13. #include <iterator>
  14. using namespace std;
  15.  
  16. int main(int argc, char* argv[]) {
  17.   requireArgs(argc, 1);
  18.   ifstream in(argv[1]);
  19.   assure(in, argv[1]);
  20.   StreamTokenizer words(in);
  21.   multiset<string> wordmset;
  22.   string word;
  23.   while((word = words.next()).size() != 0)
  24.     wordmset.insert(word);
  25.   typedef multiset<string>::iterator MSit;
  26.   MSit it = wordmset.begin();
  27.   while(it != wordmset.end()) {
  28.     pair<MSit, MSit> p=wordmset.equal_range(*it);
  29.     int count = distance(p.first, p.second);
  30.     cout << *it << ": " << count << endl;
  31.     it = p.second; // Move to the next word
  32.   }
  33. } ///:~
  34.