用STL解决单词统计程序

              以前看到有人出了一道单词统计题,意思大概是, 有一个words.txt文件的内容如下:

               some
               are
               born
               great
               some
               achieve
               greatness
               and
               some
               have
               greatness
               thrust
               upon
               them

               用程序读取该文件,统计相同的单词数,并按字符顺序写入另一个文件,如result.txt

               achieve         1
               and                1
               are                 1
               born               1
               great              1
               greatness      2
               have              1
               some            3
               them             1
               thrust            1
               upon             1

               现用两种STL方法(即vector和map)解如下:

/vector// 

Code:
  1. #include <fstream>  
  2. #include <iostream>  
  3. #include <string>  
  4. #include <vector>  
  5. #include <algorithm>   
  6. using namespace std;   
  7.   
  8. struct Count   
  9. {   
  10.     string name;   
  11.     double val;   
  12.     Count(string n="",double v=0):name(n),val(v){}   
  13. };   
  14.   
  15. bool sort_name(const Count & m1, const Count & m2)    
  16. {   
  17.     return strcmp(m1.name.c_str(),m2.name.c_str())<=0 ? true:false;   
  18. }   
  19.   
  20. vector<Count> counts;   
  21. double& value(const string& s)   
  22. {   
  23.     for (int i=0;i<counts.size();i++)   
  24.     {   
  25.         if (s==counts[i].name)   
  26.             return counts[i].val;   
  27.     }   
  28.     counts.push_back(Count(s));   
  29.   
  30.     return counts[counts.size()-1].val;   
  31. }   
  32.   
  33. int main()   
  34. {   
  35.   
  36.     ifstream ifs("words.txt");   
  37.     string buf;   
  38.   
  39.     while(ifs)   
  40.     {   
  41.         if(getline(ifs,buf))   
  42.             value(buf)++;   
  43.     }   
  44.     ifs.close();   
  45.     sort(counts.begin(), counts.end(),sort_name);   
  46.     ofstream ofs("result.txt");   
  47.     for(vector<Count>::const_iterator p=counts.begin();p!=counts.end();++p)   
  48.     {   
  49.         cout<<p->name<<'/t'<<p->val<<'/n';   
  50.         ofs<<p->name<<'/t'<<p->val<<'/n';   
  51.     }   
  52.     ofs.close();       
  53.     return 0;             
  54. }  

//map/

Code:
  1. #include <fstream>   
  2. #include <iostream>   
  3. #include <string>   
  4. #include <map>   
  5. #include <algorithm>   
  6. using namespace std;   
  7.   
  8. typedef map<string,int>::const_iterator CI;   
  9.   
  10. void Insert(map<string,int>& m)   
  11. {   
  12.     string word;   
  13.     int val=1;   
  14.     ifstream ifs("words.txt");   
  15.     while (ifs)   
  16.     {   
  17.         if(getline(ifs,word))   
  18.             m[word]+=val;   
  19.     }   
  20.     ifs.close();   
  21. }   
  22.   
  23. int main()   
  24. {   
  25.     map<string,int> tbl;   
  26.     Insert(tbl);   
  27.     ofstream ofs("result.txt");   
  28.     for (CI p=tbl.begin();p!=tbl.end();++p)   
  29.     {   
  30.         cout<<p->first<<'/t'<<p->second<<'/n';   
  31.         ofs<<p->first<<'/t'<<p->second<<'/n';   
  32.     }   
  33.     ofs.close();   
  34.     return 0;   
  35. }   

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值