C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
2018-03-30 22:20
573 查看
//.cpp文件 #include<iostream> #include<fstream> #include<sstream> #include<string> #include<vector> #include<map> #include<set> #include<iterator> #include<algorithm> ``` using namespace std; void Query(const string &word, int &numLine, map<int,string>&MapIntoStr,const vector<string>&LineString,const map<string,set<int>> &MapStoSet) { //从文本进行单词查找,将查找的信息存在我们的输入变量中 auto ret = MapStoSet.find(word); if (ret != MapStoSet.end()) { auto setin = ret->second; numLine = setin.size(); //set里元素的个数就是出现的次数,即行数 auto wz = setin.begin(); string str; while (wz != setin.end()) { str = LineString[*wz-1]; MapIntoStr.insert(make_pair(*wz, str)); ++wz; } } else numLine = 0; } void print(const string &word, int numLine,const map<int,string>&MapIntoStr) { cout <<word<< " occurs " << numLine << ((numLine> 1) ? " times" : " time") << endl; for (auto map_it = MapIntoStr.cbegin(); map_it != MapIntoStr.cend(); ++map_it) { cout << "(line " << map_it->first << ") " << map_it->second << endl; } } void getText(vector<string>&LineString, map<string, set<int>>&MapStoSet, ifstream& infile) {//从外部文件中得到需要的信息 string str; int i = 1; while(getline(infile,str)) { LineString.push_back(str); istringstream istr(str); string word; set<int>setin = {i}; while (istr >> word) { auto ret = MapStoSet.insert(make_pair(word, setin));//如果word已经存在,则什么都不做,如果没有,则用set(i)来构造对象 if (!ret.second) //如果单词已经出现在map中则将行号在原来的基础上插入行号 (ret.first)->second.insert(i); } ++i; } } int main() { ifstream infile("wordtext.txt"); vector<string>LineString;//按行保存文本 map<string, set<int>> MapStoSet;//保存每个单词出现的行号,且一行中出现多次则只记录一次 string word; //要查找的单词 int numLine; //单词出现的次数 map<int, string> MapIntoStr; //该单词出现的行以及该行所关联的文本 cout << "Please Enter word to look for" << endl; cin >> word; //输入要查询的结果 getText(LineString,MapStoSet,infile); //从外部文件中得到需要的信息 Query(word,numLine,MapIntoStr,LineString,MapStoSet); //从文本进行单词查找,将查找的信息存在我们的输入变量中 print(word, numLine, MapIntoStr); //将查询信息输出 return 0; }
相关文章推荐
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果