C++Primer笔记——文本查询程序(原创,未使用类)
2024-08-26 13:42:13
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <fstream>
#include <sstream>
#include <string> using namespace std; int main()
{
ifstream in;
in.open("C:\\Users\\HP\\Desktop\\passage.txt");
vector<string>row; //使用vector<string>来保存整个输入文件的一份拷贝,输入文件的每行保存为其中的每一个元素
map<string, set<int>>word_and_row; //使用map将每个单词与它出现的行号set关联起来,使用set可以保证行号不会重复且升序保存
string s;
while (getline(in, s))
{
row.push_back(s);
}
for (size_t i = ; i < row.size(); ++i)
{
string word;
istringstream read(row[i]); //使用istringstream来将每行分解为单词
while (read >> word)
word_and_row[word].insert(i);
} string s1; //s1为待查找的单词。注意:待查找的单词不能与句号或逗号连在一起!
while (cin >> s1 && s1 != "q" ) //输入q时终止输入
if (word_and_row.find(s1) != word_and_row.end())
{
int i = word_and_row[s1].size();
cout << s1 << " occurs " << i << " times" << endl;
for (auto d : word_and_row[s1])
cout << "(line " << d + << ") " << row[d] << endl; }
else
{
cout << "This word can not be found in this passage! Please input a word again: " << endl;
}
in.close(); return ;
}
最新文章
- Python标准模块--import
- Pywinauto在Windows Twain Driver自动化测试中的应用研究
- nyoj 2 括号配对问题
- vc6静态库的生成和调用
- iOS程序崩溃*** Terminating app due to uncaught exception &#39;CALayerInvalidGeometry&#39;, reason: &#39;CALayer position contains NaN: [37.5 nan]&#39;
- http缓存协议详解
- 使用EndNote在Word中插入参考文献的格式设置
- win10查看无线密码
- 关于IDEA每次修改HTML,Css等静态资源文件都需要重启的设置修改
- bootgrid修改成可以全勾选和全取消勾选操作
- MongoDB 聚合操作(转)
- OpenSSL开发学习总结
- 20172324《Java程序设计》第3周学习总结
- GridControl 分组排序
- 第5章 scrapy爬取知名问答网站
- pymongo和mongodbengine之间的区别
- Java内存分配及垃圾回收机制
- Python IDLE快捷键汇总
- Linux- 关于windows和Linux和Mac的换行符
- JS控制台的使用