实现词频计数器——std::map
How to do it...
#include <iostream> #include <map> #include <vector> #include <algorithm> #include <iomanip>using namespace std;string filter_punctuation(const string &s) { const char *forbidden {".,:; "}; const auto idx_start (s.find_first_not_of(forbidden)); const auto idx_end (s.find_last_not_of(forbidden)); return s.substr(idx_start, idx_end - idx_start + 1); }int main() { map<string, size_t> words; int max_word_len {0};string s; while (cin >> s) {auto filtered (filter_punctuation(s));max_word_len = max<int>(max_word_len, filtered.length());++words[filtered]; }vector<pair<string, size_t>> word_counts; word_counts.reserve(words.size()); move(begin(words), end(words), back_inserter(word_counts));sort(begin(word_counts), end(word_counts), [](const auto &a, const auto &b) { return a.second > b.second; });cout << "# " << setw(max_word_len) << "<WORD>" << " #<COUNT>\n"; for (const auto & [word, count] : word_counts) { cout << setw(max_word_len + 2) << word << " #" << count << '\n'; } }$ cat lorem_ipsum.txt | ./word_frequency_counter # <WORD> #<COUNT> et #574 dolor #302 sed #273 diam #273 sit #259 ipsum #259 ...
How it works...
Last updated