文本查询程序

简介: 我们实现一个简单的文本查询程序。我们的程序允许用户在一个给定文件中查询单词,查询结果是单词在文件中出现的次数及所在行的列表。如果一个单词在一行中出现多次,此行只列出一次。 #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?"

运行结果如下:

相关文章
|
2月前
|
机器学习/深度学习 数据采集 算法
“开水白菜”——程序应用中的上等国宴
“开水白菜”——程序应用中的上等国宴
38 0
“开水白菜”——程序应用中的上等国宴
|
小程序
关于打卡小程序可能会遇到的部分问题
关于打卡小程序可能会遇到的部分问题
85 0
关于打卡小程序可能会遇到的部分问题
对拍程序
其中data.cpp是用来生成数据的,数据保存在data.txt ac.cpp是ac的源代码 wrong.cpp是错误的代码,提前要进行编译 ac的代码结果放在ac.txt wrong的代码放在wrong.txt 如果比较有差异就会停止运行 以下是对拍的源代码
103 0
对拍程序
程序人生 - 狗狗会“嫉妒”吗?
程序人生 - 狗狗会“嫉妒”吗?
69 0
|
Web App开发 移动开发 人工智能
小程序的新战事
小程序的新战事
126 0
小程序的新战事
|
C++ 小程序
C++ 实用的小程序
1. 打开test_ids.txt 将里面的东西添加"1_",然后另存为test_ids_repaired.txt   1 #include 2 #include 3 #include 4 #include 5 #include 6 using name...
959 0
|
C++ 存储
C++电话本程序
/* C++电话本程序 */ /* 1.可实现txt文件的读取和保存。 */ /* 2.可实现联系人的增添、删除和修改。 */ /* 3.可实现通过联系人查询号码。 */ /* 4.可实现通过号码查询联系人。 */ /* 5.可实现全部联系人的显示。 */ /* 运行环境vs2010||codebl
1004 0
程序30和程序31
【程序30】题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。 $marr = array(1,3,5,7,9,11,13,15); function setPosArr($num,$arr){ for($i=count($arr);$i>0;$i--...
644 0
程序24和程序25
【程序24】 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。 $num = rand(1,99999); strlen($num); $arr_num = str_split($num); array_reverse($arr_num);strlen() 返回给定的字符串的长度。
566 0