zoukankan      html  css  js  c++  java
  • C++Primer笔记——文本查询程序(原创,未使用类)

     1 #include <iostream>
     2 #include <vector>
     3 #include <set>
     4 #include <map>
     5 #include <fstream>
     6 #include <sstream>
     7 #include <string>
     8 
     9 using namespace std;
    10 
    11 int main()
    12 {
    13     ifstream in;
    14     in.open("C:\Users\HP\Desktop\passage.txt");
    15     vector<string>row;                   //使用vector<string>来保存整个输入文件的一份拷贝,输入文件的每行保存为其中的每一个元素
    16     map<string, set<int>>word_and_row;   //使用map将每个单词与它出现的行号set关联起来,使用set可以保证行号不会重复且升序保存
    17     string s;
    18     while (getline(in, s))
    19     {
    20         row.push_back(s);
    21     }
    22     for (size_t i = 0; i < row.size(); ++i)
    23     {
    24         string word;
    25         istringstream read(row[i]);     //使用istringstream来将每行分解为单词
    26         while (read >> word)
    27             word_and_row[word].insert(i);
    28     }
    29     
    30     string s1;                          //s1为待查找的单词。注意:待查找的单词不能与句号或逗号连在一起!
    31     while (cin >> s1 && s1 != "q" )     //输入q时终止输入
    32         if (word_and_row.find(s1) != word_and_row.end())
    33         {
    34             int i = word_and_row[s1].size();
    35             cout << s1 << " occurs " << i << " times" << endl;
    36             for (auto d : word_and_row[s1])
    37                 cout << "(line " << d + 1 << ") " << row[d] << endl;
    38             
    39         }
    40         else
    41         {
    42             cout << "This word can not be found in this passage! Please input a word again: " << endl;    
    43         }
    44     in.close();
    45 
    46     return 0;
    47 }
  • 相关阅读:
    我真的没读野鸡大学!是他们不好好起名字!
    Request.Cookies和Response.Cookies
    深受理科生喜欢的10大专业
    如何玩转“互联网+教育”?
    js调试工具Console命令详解
    XSS获取cookie并利用
    257. Binary Tree Paths
    EXEC sp_executesql with multiple parameters
    235. Lowest Common Ancestor of a Binary Search Tree
    226. Invert Binary Tree
  • 原文地址:https://www.cnblogs.com/FengZeng666/p/9290082.html
Copyright © 2011-2022 走看看