#include #include #include #include #include #include using namespace std; class WordT{ public: WordT(){ count = 0; } void Word(string w) { word = w; count = 1; } string Word() const { return word; } void Increment() { count++; } int Count() const { return count; } bool operator ==(const WordT & other) { return word == other.word; } bool operator <(const WordT & other) { if (count == other.count) { return word < other.word; } return count > other.count; } private: string word; int count; }; ostream & operator << (ostream & s, const WordT & word){ s << word.Word() << " " << word.Count(); return s; } void ReadWords(ifstream & inFile, vector & words); string StripWord(string word); void PrintList(const vector & words); int main(){ ifstream inFile; vector words; vector::iterator pos; inFile.open("ocap.txt"); ReadWords(inFile, words); sort(begin(words), end(words)); for(pos = begin(words); pos != end(words); pos++) { cout <Word() << " " << pos->Count() << endl; } /* for(auto w: words) { cout < & words){ vector::const_iterator pos; for(pos = cbegin(words); pos != cend(words); pos++) { cout <Word() << " " << pos->Count() << endl; } return; } void ReadWords(ifstream & inFile, vector & words){ WordT newWord; string word; vector::iterator pos; inFile >> word; while (inFile) { word = StripWord(word); if (word != "") { newWord.Word(word); pos = find(words.begin(), words.end(), newWord); if (pos == end(words)) { words.push_back(newWord); } else { pos->Increment(); } cout << "Just processed " << newWord << endl; } inFile >> word; } inFile.close(); return ; } string StripWord(string word){ string newWord; for(auto x: word) { if(isalpha(x)) { newWord += static_cast(tolower(x)); } } return newWord; }