zoukankan      html  css  js  c++  java
  • 文本查询程序

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

    #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?"

    运行结果如下:

  • 相关阅读:
    Python pip – error: invalid command ‘bdist_wheel’
    css实现文字两端对齐(兼容所有浏览器)
    webpack中的require.context
    node 的path模块中 path.resolve()和path.join()的区别
    react-native项目中遇到的问题
    react-native针对android改变状态栏样式
    createBottomTabNavigator: 怎么在切换tab的时候让页面重新渲染
    当vue页面异步加载的数据想在页面上渲染怎么办
    git分布式版本控制系统
    $router和$route的区别,路由跳转方式name 、 path 和传参方式params 、query的区别
  • 原文地址:https://www.cnblogs.com/wuchanming/p/3923516.html
Copyright © 2011-2022 走看看