如何统计出一篇文章出现的文字个数? (高级) (使用std::map)

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /* 
 2InBlock.gif(C) OOMusou 2006 http://oomusou.cnblogs.com
 3InBlock.gif
 4InBlock.gifFilename    : CountRepeaterByWordByMap.cpp
 5InBlock.gifCompiler    : Visual C++ 8.0
 6InBlock.gifDescription : Demo how to count repeated words by std::map
 7InBlock.gifRelease     : 11/16/2006
 8ExpandedBlockEnd.gif*/

 9 None.gif
10 None.gif#include  < iostream >
11 None.gif#include  < fstream >
12 None.gif#include  < cctype >
13 None.gif#include  < string >
14 None.gif#include  < map >
15 None.gif#include  < algorithm >
16 None.gif
17 None.gif void  repeatedWords(std:: string );
18 None.gif
19 ExpandedBlockStart.gifContractedBlock.gif int  main()  dot.gif {
20InBlock.gif  std::string fileName("textfile.txt");
21InBlock.gif  repeatedWords(fileName);
22InBlock.gif
23InBlock.gif  return 0;
24ExpandedBlockEnd.gif}

25 None.gif
26 ExpandedBlockStart.gifContractedBlock.gif void  repeatedWords(std:: string  fileName)  dot.gif {
27InBlock.gif
28InBlock.gif  std::ifstream infile(fileName.c_str());
29InBlock.gif
30ExpandedSubBlockStart.gifContractedSubBlock.gif  if (!infile) dot.gif{
31InBlock.gif    std::cout << "Read error!!" << std::endl;
32InBlock.gif
33InBlock.gif    return;
34ExpandedSubBlockEnd.gif  }

35InBlock.gif
36InBlock.gif  std::map<std::stringint> wordCount;
37ExpandedSubBlockStart.gifContractedSubBlock.gif  for(std::string word, newWord; infile >> word; ++wordCount[newWord], newWord=""dot.gif{
38ExpandedSubBlockStart.gifContractedSubBlock.gif    for(std::string::iterator iter = word.begin(); iter != word.end(); ++iter) dot.gif{
39ExpandedSubBlockStart.gifContractedSubBlock.gif        if (!ispunct(*iter)) dot.gif{
40InBlock.gif          newWord.push_back(tolower(*iter));
41ExpandedSubBlockEnd.gif        }

42ExpandedSubBlockEnd.gif    }

43ExpandedSubBlockEnd.gif  }

44InBlock.gif
45InBlock.gif  infile.close();
46InBlock.gif
47InBlock.gif  // cout the result
48InBlock.gif  for(std::map<std::stringint>::iterator iter = wordCount.begin();
49ExpandedSubBlockStart.gifContractedSubBlock.gif    iter != wordCount.end(); ++iter) dot.gif{
50InBlock.gif      std::cout << "The Word '" << iter->first << "' Occurrence:" << iter->second << " times" << std::endl;
51ExpandedSubBlockEnd.gif  }

52ExpandedBlockEnd.gif}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值