词索引表

 STL实现,好久没看STL了,做个当温习一下,也是自己怕书上的算法麻烦偷懒下哈

 

 

BookInfo.txt文件
005 Computer Data Structures
010 Introduction to Data Structures
023 Fundamentals of Data Structures
034 The Design and Analysis of Computer Algorithms
050 Introduction to Numerical Analysis
067 Numerical Analysis

输出为BookInfo.txt文件 (关键词按字典顺序)

algorithms  034
analysis      034,050,067
computer    005,034
data             005,010,023
design          034
fundamentals  023
introduction  010,050
numerical    050,067
structures  005,010,023

 

  1. #include<iostream>
  2. #include<map>
  3. #include<vector>
  4. #include<stdlib.h>
  5. #include<fstream>
  6. #include<sstream>
  7. #include<set>
  8. using namespace std;
  9. typedef  map<string,vector<string> >  mp;
  10. typedef  vector<string>   vs;
  11. string ignorstr[]={"an","a","of","the","and","to"};
  12. int main()
  13. {
  14.     mp IdxList;
  15.     ifstream in("d://BookInfo.txt",ios::in);
  16.     ofstream out("d://BookIdx.txt",ios::out);
  17.     string s,ss,bno;
  18.     set<string> strset(ignorstr,ignorstr+6);
  19.     int i;
  20.     while(getline(in,s))
  21.     {  
  22.        transform(s.begin(), s.end(), s.begin(), ::tolower); 
  23.        stringstream strs(s);
  24.        i=0;
  25.        while(strs>>ss)
  26.         {
  27.           if(!i) bno=ss;
  28.           else if(!strset.count(ss)) IdxList[ss].push_back(bno);
  29.           i++; }
  30.           }
  31.     for(mp::iterator frm=IdxList.begin();frm!=IdxList.end();frm++)
  32.      {
  33.       out<<(*frm).first<<"/t/t";
  34.       for(vs::iterator vfrm=(*frm).second.begin();vfrm!=(*frm).second.end();vfrm++)
  35.       {out<<*vfrm; 
  36.        vfrm+1==(*frm).second.end()?out<<endl:out<<",";}
  37.                   }
  38.       in.close();
  39.       out.close();
  40.       return 0;
  41.        }
  42.        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值