文本查询程序

简介: 我们实现一个简单的文本查询程序。我们的程序允许用户在一个给定文件中查询单词,查询结果是单词在文件中出现的次数及所在行的列表。如果一个单词在一行中出现多次,此行只列出一次。 #include #include #include #include #include #include #...

我们实现一个简单的文本查询程序。我们的程序允许用户在一个给定文件中查询单词,查询结果是单词在文件中出现的次数及所在行的列表。如果一个单词在一行中出现多次,此行只列出一次。

#include<iostream>
#include<map>
#include<set>
#include<string>
#include<vector>
#include<fstream>
#include<sstream>
#include<algorithm>
using namespace std;
//主要完成将输入文件转换为map形式
map<int,vector<string>> runQueries(ifstream &infile)
{
    map<int,vector<string>> map_word;
    int lines=0;
    string text;
    while(getline(infile,text))
    {
        istringstream in(text);
        string word;
        while(in>>word)
        {
            map_word[lines].push_back(word);
        }
        ++lines;
    }
    return map_word;
}

int main()
{
    ifstream in("1.txt");
    map<int,vector<string>> map_word=runQueries(in);
    set<int> iset;
    string str;
    cin>>str;
    vector<string>::size_type lines=0;
    while(lines!=map_word.size())
    {
        vector<string> tmp=map_word[lines];
        auto iter=find(tmp.begin(),tmp.end(),str);
        if(iter!=tmp.end())
           iset.insert(lines);
        lines++;
    }
    cout<<"elements occurs "<<iset.size()<<" times "<<endl;
    auto ss=iset.begin();
    while(ss!=iset.end())
    {
        cout<<"(line "<<*ss<<" )"<<" ";
        for(auto v:map_word[*ss])
            cout<<v<<" ";
        cout<<endl;
        ++ss;
    }

    return 0;
}

1.txt

Alice Emma has long flowing red hair.
Her Daddy says when the wind blows
through her hair, it looks almost alive,
like a fiery bird in flight.
A beautiful fiery bird, he tells her,
magical but untamed.
"Daddy, shush, there is no such thing,"
she tells him, at the same time wanting
him to tell her more.
Shyly, she asks, "I mean, Daddy, is there?"

Alice Emma has long flowing red hair.
Her Daddy says when the wind blows
through her hair, it looks almost alive,
like a fiery bird in flight.
A beautiful fiery bird, he tells her,
magical but untamed.
"Daddy, shush, there is no such thing,"
she tells him, at the same time wanting
him to tell her more.
Shyly, she asks, "I mean, Daddy, is there?"

Alice Emma has long flowing red hair.
Her Daddy says when the wind blows
through her hair, it looks almost alive,
like a fiery bird in flight.
A beautiful fiery bird, he tells her,
magical but untamed.
"Daddy, shush, there is no such thing,"
she tells him, at the same time wanting
him to tell her more.
Shyly, she asks, "I mean, Daddy, is there?"

运行结果如下:

相关文章
|
8月前
|
机器学习/深度学习 数据采集 算法
“开水白菜”——程序应用中的上等国宴
“开水白菜”——程序应用中的上等国宴
77 0
“开水白菜”——程序应用中的上等国宴
|
小程序
关于打卡小程序可能会遇到的部分问题
关于打卡小程序可能会遇到的部分问题
123 0
关于打卡小程序可能会遇到的部分问题
|
存储 小程序 JavaScript
小程序 globalData
小程序 globalData
92 0
对拍程序
其中data.cpp是用来生成数据的,数据保存在data.txt ac.cpp是ac的源代码 wrong.cpp是错误的代码,提前要进行编译 ac的代码结果放在ac.txt wrong的代码放在wrong.txt 如果比较有差异就会停止运行 以下是对拍的源代码
144 0
对拍程序
程序人生 - 2025年社保卡将覆盖全国
程序人生 - 2025年社保卡将覆盖全国
116 0
程序人生 - 2025年社保卡将覆盖全国
程序人生 - 狗狗会“嫉妒”吗?
程序人生 - 狗狗会“嫉妒”吗?
96 0
程序人生 - 一直打嗝怎么办?
程序人生 - 一直打嗝怎么办?
122 0
|
移动开发 小程序 开发者
初步了解小程序
初步了解小程序
214 0
初步了解小程序
如何跳小程序
       经常会有人遇到这样的疑惑——        我有自己的APP、生活号或在支付宝端内有自己的H5页面,这种情况下如何和小程序关联,跳转到小程序里去,做到无缝对接?        其实,小程序是支持这类跳转能力的;        那么,要如何实现呢?        一、非小程序前端——跳转小程序 window.
666 12