vector set 用法示例

Posted wuguoqiang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vector set 用法示例相关的知识,希望对你有一定的参考价值。

实现功能:

在指定文件中查找输入字符串所在的行

 

 TextQuery.h

#pragma once

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <fstream>
#include <sstream>



class TextQuery
{
public:
    TextQuery(void);
    ~TextQuery(void);

    typedef std::vector <std::string>::size_type line_no;

    void read_file(std::ifstream &is)
    {
        store_file(is);
        build_map();

    }

    std::string text_line(line_no) const;

    std::set<TextQuery::line_no> run_query(const std::string &query_word) const;

private:
    void store_file(std::ifstream &is);

    void build_map();

    std::vector <std::string> lines_of_text;    //line string of txt

    std::map < std::string, std::set<line_no> > word_map;
};

 

 

TextQuery.cpp

#include "TextQuery.h"


TextQuery::TextQuery(void)
{
}


TextQuery::~TextQuery(void)
{
}



void TextQuery::store_file(std::ifstream &is)
{
    std::string textline;

    while(getline(is, textline))
    {
        lines_of_text.push_back(textline);
    }
}


void TextQuery::build_map()
{
    for(line_no line_num = 0; line_num != lines_of_text.size(); ++line_num)
    {
        std::istringstream line(lines_of_text[line_num]);

        std::string word;

        while(line >> word)
        {
            word_map[word].insert(line_num);
        }
    }
}

std::set<TextQuery::line_no> TextQuery::run_query(const std::string &query_word) const
{
    std::map<std::string, std::set<line_no>>::const_iterator loc = word_map.find(query_word);

    if(loc == word_map.end())
    {
        return std::set<line_no>();
    }else
    {
        return loc->second;
    }
}

std::string TextQuery::text_line(line_no line) const
{
    if(line < lines_of_text.size())
    {
        return lines_of_text[line];
    }else
    {
        throw::std::out_of_range("line number out of range");
    }
}


main.cpp

#include <iostream>

#include "TextQuery.h"


std::string make_plural(std::set<TextQuery::line_no>::size_type sizetemp, std::string str1, std::string str2)
{
    if(sizetemp <= 1)
    {
        return str1;
    }else
    {
        return str1 + str2;
    }
}

void print_results(const std::set<TextQuery::line_no> &locs, 
                                const std::string &sought, const TextQuery &file)
{
    typedef std::set<TextQuery::line_no> line_nums;

    line_nums::size_type size = locs.size();

    std::cout << "
" << sought << " occurs " << size << " " << make_plural(size, "time", "s") << std::endl;

    line_nums::const_iterator it = locs.begin();

    for(; it != locs.end(); ++it)
    {
        std::cout << "	(line " << (*it) +1 << ") " << file.text_line(*it) << std::endl;
    }
}

std::ifstream& open_file(std::ifstream &in, const std::string &file)
{
    in.close();
    in.clear();
    in.open(file.c_str());
    return in;
}




int main(int argc, char* argv[])
{

    std::ifstream infile;

    if(/*argc < 2 ||*/ !open_file(infile, "D:\\TEXT.txt"))
    {
        std::cerr << "No input file!" << std::endl;

        return EXIT_FAILURE;
    }

    TextQuery tq;

    tq.read_file(infile);

    while(true)
    {
        std::cout << "enter word to look for, or q to quit: ";
        std::string s;
        std::cin >> s;

        if(!std::cin || s == "q") break;

        std::set<TextQuery::line_no> locs = tq.run_query(s);

        print_results(locs, s, tq);
    }
    
    

    return 0;
}

技术图片

以上是关于vector set 用法示例的主要内容,如果未能解决你的问题,请参考以下文章

请问 -> 的用法和这是啥类型的构造?

c++里面std::vector的用法!如果我使用vector<Object>,怎么对立面的每个元素进行Get/Set/Remove等操作?

STL常用容器:vector容器用法总结

STL容器用法速查表:list,vector,stack,queue,deque,priority_queue,set,map

leetcode 249 250 set和map的简单用法

c_cpp kstat_lookup和kstat_read用法的示例kstat片段