本文共 1687 字,大约阅读时间需要 5 分钟。
注意:文中包含中文会出错!!!
#include #include #include #include #include #include #include #include using namespace std;//将字符串分割成单词vector Split(const string & s);//用于向量排序的比较bool cmp(const pair & x, const pair & y);int main(){ //输入文件路径 string filePath; getline(cin, filePath); //打开文件 ifstream in(filePath/*"2012CET4.txt"*/); string line; //读取一行字符串 map counters; //统计单词出现次数 while (getline(in, line)) { vector v = Split(line); for (vector ::iterator it = v.begin(); it != v.end(); ++it) { ++counters[*it]; } } //对结果按统计次数排序 vector > sv; for (map ::const_iterator it = counters.begin(); it != counters.end(); ++it) { sv.push_back(make_pair(it->first, it->second)); } sort(sv.begin(), sv.end(), cmp); //打印结果 for (vector >::const_iterator it = sv.begin(); it != sv.end(); ++it) { cout << setiosflags(ios::left) << setw(20) << it->first << "\t" << it->second << endl; } //保存结果 ofstream out("counts.txt"); for (vector >::const_iterator it = sv.begin(); it != sv.end(); ++it) { out << setiosflags(ios::left) << setw(20) << it->first << "\t" << it->second << endl; } out.close(); cin.get(); return 0;}vector Split(const string & s){ vector ret; typedef string::size_type string_size; string_size i = 0; while (i != s.size()) { //忽略字符串开始的空白符 while (i != s.size() && isspace(s[i])) ++i; string_size j = i; while (j != s.size() && !isspace(s[j])) ++j; //存储单词,并去除单词最后一位可能的标点符号 if (i != j) { ret.push_back(s.substr(i, ispunct(s[j - 1]) ? j - i - 1 : j - i)); i = j; } } return ret;}bool cmp(const pair & x, const pair & y){ return x.second > y.second;}
转载于:https://www.cnblogs.com/BornThisWay/p/CountWords.html