| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 296 人关注过本帖
标题:帮忙看一下问题
只看楼主 加入收藏
sealan
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2009-7-14
结帖率:100%
收藏
 问题点数:0 回复次数:0 
帮忙看一下问题
头文件:
#include <vector>
#include <string>
#include <map>
#include <set>
#include <sstream>
#include <iostream>
using namespace std;

class TextQuery
{
public:
    typedef vector<string>::size_type line_no;
    void read_file(ifstream &is)
    {
        store_file(is);
        build_map();
    }
    set<line_no> run_query(const string&) const;
    string text_line(line_no) const;
public:
    void store_file(ifstream &);
    void build_map();
    vector<string> lines_of_text;
    map<string, set<line_no> > word_map;
};
#endif

void TextQuery::store_file(ifstream &is)
{
    string textline;
    while (std::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)
    {
        istringstream line(lines_of_text[line_num]);
        string word;
        while (line >> word)
            word_map[word].insert(line_num);
    }
}

set<TextQuery::line_no> TextQuery::run_query(const string &query_word) const
{
    map<string, set<line_no>::const_iterator loc = word_map.find(query_word);
    if (loc == word_map.end())
        return set<line_no>();
    else
        return loc->second;
}

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

源文件:
#include "TextQuery.h"

void print_results(const set<TextQuery::line_no>& locs,
                   const string& sought, const TextQuery &file)
{
    typedef set<TextQuery::line_no> line_nums;
    line_nums::size_type size = locs.size();
    cout << "\n" << sought << " occurs " << size << " "
         << make_plural(size, "time", "s") << endl;
    line_nums::const_iterator it = locs.begin();
    for ( ; it != locs.end(); ++it)
    {
        cout << "\t(line " << (*it) + 1 << ") " << file.text_line(*it) << endl;
    }
}

int main(int argc, char **argv)
{
    ifstream infile;
    if (argc < 2 || !open_file(infile, argv[1]))
    {
        cerr << "No input file!" << endl;
        return EXIT_FAILURE;
    }
    TextQuery tp;
    tq.read_file(infile);

    while (true)
    {
        cout << "enter word to look for, or q to quit: " << endl;
        string s;
        cin >> s;
        if (!cin || s == 'q')
            break;
        set<TextQuery::line_no> locs = tq.run_query(s);
        print_results(locs, s, tq);
    }
    return 0;
}


编译错误,getline怎么错了
2009-07-14 22:32
快速回复:帮忙看一下问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017987 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved