#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 ;
}

最新文章

  1. Python标准模块--import
  2. Pywinauto在Windows Twain Driver自动化测试中的应用研究
  3. nyoj 2 括号配对问题
  4. vc6静态库的生成和调用
  5. iOS程序崩溃*** Terminating app due to uncaught exception &#39;CALayerInvalidGeometry&#39;, reason: &#39;CALayer position contains NaN: [37.5 nan]&#39;
  6. http缓存协议详解
  7. 使用EndNote在Word中插入参考文献的格式设置
  8. win10查看无线密码
  9. 关于IDEA每次修改HTML,Css等静态资源文件都需要重启的设置修改
  10. bootgrid修改成可以全勾选和全取消勾选操作
  11. MongoDB 聚合操作(转)
  12. OpenSSL开发学习总结
  13. 20172324《Java程序设计》第3周学习总结
  14. GridControl 分组排序
  15. 第5章 scrapy爬取知名问答网站
  16. pymongo和mongodbengine之间的区别
  17. Java内存分配及垃圾回收机制
  18. Python IDLE快捷键汇总
  19. Linux- 关于windows和Linux和Mac的换行符
  20. JS控制台的使用

热门文章

  1. how tomcat works
  2. SQL Server 备份还原
  3. ES系列四、ES6.3常用api之文档类api
  4. 【转】Visual Studio——多字节编码与Unicode码
  5. 罗克韦尔 Allen-Bradley MicroLogix 1400 查看、设置IP
  6. POJ 3243 // HDU 2815(改下输出,加个判断)
  7. wampserver修改端口号后,phpMyAdmin进不去,需要修改相关配置文件
  8. Linux 快速删除已输入的命令
  9. js获取iframe中的元素
  10. sql基础笔记备忘