/ Published in: C++

Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include <fstream> #include <iostream> #include <set> #include <string> using namespace std; struct comparator { bool operator()(const string &a, const string &b) { if(a.length() == b.length()) return a<b; return a.length()<b.length(); } }; int main() { ifstream in("/usr/share/dict/words"); set<string,comparator> sorter; string s; while(in >> s) if(tolower(s[0]) == 'q') sorter.insert(s); int max = 0; for(set<string,comparator>::iterator word = sorter.begin(); word != sorter.end(); ++word) { if(word->length() > max) { max = word->length(); cout << "Words of length " << max << '\n'; } cout << *word << " " << word->length() << "\n"; } return 0; }
URL: http://steve.yegge.googlepages.com/ruby-tour
Comments
