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
- #include<iostream>
- #include<map>
- #include<vector>
- #include<stdlib.h>
- #include<fstream>
- #include<sstream>
- #include<set>
- using namespace std;
- typedef map<string,vector<string> > mp;
- typedef vector<string> vs;
- string ignorstr[]={"an","a","of","the","and","to"};
- int main()
- {
- mp IdxList;
- ifstream in("d://BookInfo.txt",ios::in);
- ofstream out("d://BookIdx.txt",ios::out);
- string s,ss,bno;
- set<string> strset(ignorstr,ignorstr+6);
- int i;
- while(getline(in,s))
- {
- transform(s.begin(), s.end(), s.begin(), ::tolower);
- stringstream strs(s);
- i=0;
- while(strs>>ss)
- {
- if(!i) bno=ss;
- else if(!strset.count(ss)) IdxList[ss].push_back(bno);
- i++; }
- }
- for(mp::iterator frm=IdxList.begin();frm!=IdxList.end();frm++)
- {
- out<<(*frm).first<<"/t/t";
- for(vs::iterator vfrm=(*frm).second.begin();vfrm!=(*frm).second.end();vfrm++)
- {out<<*vfrm;
- vfrm+1==(*frm).second.end()?out<<endl:out<<",";}
- }
- in.close();
- out.close();
- return 0;
- }