#include #include #include #include #include #include using namespace std; bool comparePairs( const pair& i0, const pair& i1 ); int main() { map words; string word; bool readingWord = false; while (true) { // char c; // cin >> c; string line; std::getline(cin, line); if (!cin.good()) break; for (size_t i = 0; i < line.size(); ++i) { char c = line[i]; if (readingWord) { if (isalnum(c) || c == '_') { word += c; } else { // End of current word readingWord = false; if (words.count(word) == 0) { // Add a word to the dictionary words[word] = 1; } else { // Increment the number of such words in a text ++(words[word]); } // cout << word << endl; } } else { if (isalpha(c) || c == '_') { word.clear(); word += c; readingWord = true; } } } // end for } // end while // Print words in alphabet lexicographic order vector< pair > wordFreq; for (auto i = words.cbegin(); i != words.cend(); ++i) { // cout << i->first << " " << i->second << endl; wordFreq.push_back(*i); } cout << "Number of different words in a text: " << wordFreq.size() << endl; stable_sort(wordFreq.begin(), wordFreq.end(), comparePairs); for (auto i = wordFreq.cbegin(); i != wordFreq.cend(); ++i) { cout << i->second << "\t" << i->first << endl; } return 0; } bool comparePairs( const pair& i0, const pair& i1 ) { return (i0.second > i1.second); }