C++ primer testQuery
2014-06-01 16:21
162 查看
</pre><p><pre name="code" class="cpp">#include<iostream> #include<string> #include<fstream> #include<sstream> #include<vector> #include<map> #include<set> #include<memory> using namespace std; int main() { ifstream infile("data.txt"); map<string,shared_ptr<set<size_t>>> wm; shared_ptr<vector<string>> file(new vector<string>); string line; while(getline(infile,line)) { file->push_back(line); int n=file->size()-1; istringstream line_word(line); string word; while(line_word>>word) { auto &set_line=wm[word]; if(!set_line) set_line.reset(new set<size_t>); set_line->insert(n); } } string sought; shared_ptr<set<size_t>> nodata(new set<size_t>); while(1) { cout<<"enter a word: "; cin>>sought; if(sought=="q") break; auto loc_set=wm.find(sought); if(loc_set!=wm.end()) { cout<<sought<<" "<<(loc_set->second)->size()<<" time "<<endl; for (auto num:(*(loc_set->second))) cout<<"line "<<num+1<<" "<<*(file->begin()+num)<<endl; } else { cout<<"no data"<<endl; } } return 0; }
改了书上的代码。没有用到类。只是为了熟悉文件输入输出操作以及容器的使用
相关文章推荐
- c++_primer_exercise_1531_Query
- C++ Primer TextQuery 完整代码
- 整理的C++ primer TextQuery 例题
- Google C++ Test Framework (Primer)
- 《C++.Primer.Plus》学习笔记(一)
- C++ primer plus 5th 学习日记
- [学习笔记][C++Primer Plus]String类的使用
- C++ Primer notes: Chapter 2. Variables and Basic Types
- C++ primer plus 第五版 6章4题
- C++ Primer 学习笔记关于练习题6.12
- C++ Primer Plus 中文版下载
- C++ Primer Plus 读书笔记 (1 -2 章)
- c++头文件,cpp文件,makefile,unit test自动生成器
- 介绍一个轻量级的C++ Unit test 框架: TUT
- [c++ primer plus]使用标准流格式化输入
- 读c++primer
- (摘)C++ Primer Plus, Fourth Edition---Using a Dynamic Array
- C++ primer plus 5章9题
- a test program for polymophism in c++
- [c++ primer plus]使用cout格式化输出