词索引表

来源:互联网 发布:不停弹出windows media 编辑:程序博客网 时间:2024/04/28 03:13

 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.        
原创粉丝点击