#include #include #include #include #include using namespace std; string trim(string s){ if(s.empty()) return s; s.erase(0,s.find_first_not_of(" ")); s.erase(s.find_last_not_of(" ") + 1); return s; } void readTxt(string file){ std:map newMap; ifstream infile; infile.open(file.data()); //将文件流对象与文件连接起来 char c; infile >> noskipws; //不忽略空白字符,将其读取 string stopword = " ,;?.!0123456789"; string tmp = ""; while (!infile.eof()) { infile >> c; if(stopword.find(c) != string::npos){ tmp = trim(tmp); if(tmp.length() == 0) continue; if(niuMap.count(tmp) > 0){ niuMap[tmp] += 1; } else{ niuMap.insert(map::value_type (tmp, 1)); } tmp = ""; } else{ tmp += c; } } infile.close(); //关闭文件输入流 for(auto it = newMap.begin(); it != newMap.end(); ++it){ cout << it->first << ":" << it->second << endl; } } int main(){ string a; cin >> a; readTxt(a); }
上一篇 python第一天作业
下一篇 C语言入门项目:三子棋小游戏(详解)
版权所有 (c)2021-2022 MSHXW.COM
ICP备案号:晋ICP备2021003244-6号