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;
- }
本文介绍了一个使用STL进行图书信息处理的程序。该程序读取包含书籍编号及名称的文件,并通过C++标准模板库(STL)中的数据结构对图书信息进行排序和索引生成。处理过程中忽略了常见词汇,最终输出关键词及其对应的图书编号。
787

被折叠的 条评论
为什么被折叠?



