读取文件, 统计字符,忽略大小写

来源:互联网 发布:mac轻淘客插件 编辑:程序博客网 时间:2024/05/16 01:34
int main (int argc, char* argv[])
{
   std::ios::sync_with_stdio (false);
    ifstreamfile;
    file.open(argv[1]);
    mapword_count;
    string word,line;
    while(getline (file, line)) {
      istringstream record (line);
       while(record >> word) {
          for (auto&ch : word)
             ch = tolower(ch);
          word.erase(remove_if (word.begin (), word.end (), [] (const char&ch){return ispunct (ch); }));
         ++word_count[word];
       }
    }
    for (constauto &w : word_count) {
       cout<< w.first << " ocurrs " << w.second;
       cout<< ((w.second > 1) ? " times" : " time") <<endl;
    }
    return0;
}
0 0