编程珠玑:单词频率最高选取

问题描述:

对一个输入文本中的每个单词的出现次数统计,,并选取出现频率最大的10个单词

#include <iostream> #include <stdlib.h> #include <string> #include <map> #include <vector> #include <algorithm>using namespace std; typedef pair<string, int> PAIR;int cmp(const PAIR& x, const PAIR& y){return x.second > y.second;}int main() {map<string, int> wordcount;vector<PAIR> vec;vector<PAIR>::iterator itr;string t;){wordcount[t]++;}for (map<string,int>::iterator curr = wordcount.begin(); curr != wordcount.end(); ++curr){vec.push_back(make_pair(curr->first, curr->second));}sort(vec.begin(), vec.end(), cmp);for(itr = vec.begin(); itr != vec.end(); itr++){cout << itr->first << " " << itr->second<<endl;}system(); }

伟人之所以伟大,是因为他与别人共处逆境时,

编程珠玑:单词频率最高选取

相关文章:

你感兴趣的文章:

标签云: