Stevey's sorting program in C++


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #include <fstream>
  2. #include <iostream>
  3. #include <set>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. struct comparator {
  9. bool operator()(const string &a, const string &b) {
  10. if(a.length() == b.length())
  11. return a<b;
  12. return a.length()<b.length();
  13. }
  14. };
  15.  
  16. int main()
  17. {
  18. ifstream in("/usr/share/dict/words");
  19. set<string,comparator> sorter;
  20. string s;
  21.  
  22. while(in >> s) if(tolower(s[0]) == 'q') sorter.insert(s);
  23.  
  24. int max = 0;
  25. for(set<string,comparator>::iterator word = sorter.begin(); word != sorter.end(); ++word) {
  26. if(word->length() > max) {
  27. max = word->length();
  28. cout << "Words of length " << max << '\n';
  29. }
  30. cout << *word << " " << word->length() << "\n";
  31. }
  32. return 0;
  33. }

URL: http://steve.yegge.googlepages.com/ruby-tour

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.