问题描述:
对一个输入文本中的每个单词的出现次数统计,并选取出现频率最大的10个单词
<span style="font-size:18px;">#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; while((cin >> t) && t != "0") { 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("pause"); } </span>