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

 

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /* 
 2InBlock.gif(C) OOMusou 2006 http://oomusou.cnblogs.com
 3InBlock.gif
 4InBlock.gifFilename    : CountRepeatedWordByVector.cpp
 5InBlock.gifCompiler    : Visual C++ 8.0
 6InBlock.gifDescription : Demo how to count repeated words by vector
 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  < vector >
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::vector<std::string> wordsVec;
37InBlock.gif
38ExpandedSubBlockStart.gifContractedSubBlock.gif  for(std::string word, newWord; infile >> word; newWord = ""dot.gif{
39InBlock.gif    for(std::string::iterator iter = word.begin();
40ExpandedSubBlockStart.gifContractedSubBlock.gif      iter != word.end(); ++iter) dot.gif{
41InBlock.gif
42ExpandedSubBlockStart.gifContractedSubBlock.gif        if (!ispunct(*iter)) dot.gif{
43InBlock.gif          newWord.push_back(tolower(*iter));
44ExpandedSubBlockEnd.gif        }

45ExpandedSubBlockEnd.gif     }

46InBlock.gif     
47InBlock.gif     wordsVec.push_back(newWord);
48ExpandedSubBlockEnd.gif  }

49InBlock.gif
50InBlock.gif  infile.close();
51InBlock.gif
52InBlock.gif  // unique_copy output vector must be initialized first
53InBlock.gif  std::vector<std::string> uniqueWordsVec(wordsVec.size());
54InBlock.gif  // Must sort before using unique_copy
55InBlock.gif  sort(wordsVec.begin(), wordsVec.end());
56InBlock.gif  // unique_copy algorithm to uniqueWordsVec
57InBlock.gif  std::vector<std::string>::const_iterator endIter = unique_copy(wordsVec.begin(), wordsVec.end(), uniqueWordsVec.begin());
58InBlock.gif
59ExpandedSubBlockStart.gifContractedSubBlock.gif  for(std::vector<std::string>::const_iterator iter = uniqueWordsVec.begin(); iter != endIter; ++iter) dot.gif{
60InBlock.gif      // count algorithm to count
61InBlock.gif      std::cout << "The Word '" << *iter << "' Occurrence:" << (size_t)count(wordsVec.begin(), wordsVec.end(),*iter) << " times" << std::endl;
62ExpandedSubBlockEnd.gif  }

63ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值