C/C++:文本查询(单词查询)
如题:
C/C++:
Textqurey.h(方便看都在.h里实现了):
1 // 2 // Created by 徐爱东 on 17/7/10. 3 // 4 5 #ifndef TEXTQUERY_TEXTQUERY_H 6 #define TEXTQUERY_TEXTQUERY_H 7 8 #include <iostream> 9 #include <memory> 10 #include <vector> 11 #include <map> 12 #include <set> 13 #include <string> 14 #include <sstream> 15 #include <fstream> 16 17 18 class Textquery 19 { 20 private: 21 using vsizetype=std::vector<std::string>::size_type; 22 std::shared_ptr<std::vector<std::string> > File; 23 std::map<std::string, std::shared_ptr<std::set<vsizetype> > > wm; 24 25 public: 26 class QueryResult 27 { 28 friend class Textquery; 29 30 private: 31 std::string Queryword; 32 std::shared_ptr<std::vector<std::string> > file_; 33 std::shared_ptr<std::set<vsizetype> > lines_; 34 public: 35 QueryResult(std::string Queryword, std::shared_ptr<std::vector<std::string> > file, 36 std::shared_ptr<std::set<vsizetype> > lines) 37 : Queryword(Queryword), 38 file_(file), 39 lines_(lines) 40 { 41 42 } 43 }; 44 45 Textquery(std::fstream &infile) 46 : File(new std::vector<std::string>) 47 { 48 //文本存入vector并计算单词行数入set 49 std::string Textline; 50 std::stringstream stream; 51 while (std::getline(infile, Textline)) 52 { 53 stream.clear(); //标志位清理 54 File->push_back(Textline); 55 stream << Textline; 56 vsizetype linenumber = File->size() - 1; //行号; 57 58 while (stream >> Textline) //单词根据空格分隔 59 { 60 auto &lines = wm[Textline]; //map[]方式没有则创建,有则返回value_type; 61 if (!lines) //指针为空,则创建 62 lines.reset(new std::set<vsizetype>); 63 lines->insert(linenumber); 64 } 65 } 66 } 67 68 69 QueryResult query(const std::string &str) 70 { 71 auto loca=wm.find(str); 72 73 if ( loca!= wm.end()) 74 { 75 return QueryResult(str, File, wm[str]); 76 } else 77 { 78 std::shared_ptr<std::set<vsizetype>> shared_null(new std::set<vsizetype >); //没找到则返回空指针,防止崩溃 79 return QueryResult(str, File, shared_null); 80 } 81 } 82 83 std::ostream &display(std::ostream &out, QueryResult result) 84 { 85 if (!result.lines_) 86 { 87 return out << result.Queryword << " appear 0 time\n"; 88 89 } else 90 { 91 out << result.Queryword << " appear " << result.lines_->size() 92 << (result.lines_->size() > 1 ? " times" : " time") << "\n"; 93 94 95 for (auto &iter:*result.lines_) 96 { 97 out << "\t\t\t\t\t(line" << iter + 1 << "): " << *(result.file_->begin() + iter) << std::endl; 98 } 99 100 //不能- -for_each, lamda语法报错? 101 // std::for_each(result.lines_->begin(), result.lines_->end(), [&](vsizetype &linenumber) 102 // { 103 // out<<"\t(line"<<linenumber+1<<"): "<<*(result.file_->begin()+linenumber)<<std::endl; 104 // }); 105 106 return out; 107 } 108 } 109 }; 110 111 112 #endif //TEXTQUERY_TEXTQUERY_H
main.cpp:
#include <iostream> #include "Textquery.h" int main() { std::fstream file("./Tex.txt", std::ios::in); std::stringstream stream; Textquery query(file); std::string word; do { std::cout << "Please input search word or q to quit: "; std::cin >> word; if (word == "q")break; query.query(word); query.display(std::cout, query.query(word)); } while (1); std::cout<<"end"<<std::endl; return 0; }
运行结果: