目的:熟悉各种关联容器的使用以及面向具体任务时程序的设计流程。
参考:c++primer 10.6
基本步骤:用户需求 -> 明确任务 -> 数据结构以及相关操作 ->定义相关类 ->根据操作定义类成员函数
任务描述:从已知文档中查询某个单词出现的次数,并列出所在行
任务分工:1.需要有一个函数来读取指定文件,将其存储在向量中并用一个map记录每个单词的所在行,map.first是单词,map.second 是一个由行号组成的set;
2.需要一个查询函数,对特定的单词返回其行号的set;
3.需要一个输出查询结果的函数(这个是对外接口,放在主函数中较为合适)。
数据格式的使用:vector非常适合存储和遍历;而map能提供方便的查询接口;set中存储所在行号的集合保证了唯一性和有序性。
具体代码如下:
TextQuery.h
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <stdexcept>
class TextQuery
{
public:
TextQuery();
~TextQuery();
typedef std::vector<std::string>::size_type line_no;//为每一行的索引定义一个别名
void read_file(std::ifstream & is);//读取文件
private:
void store_file(std::ifstream & is);//这两个函数对外不可见,被成员函数read_file调用
std::vector<std::string> lines_of_text;
private:
void build_map();//这两个函数对外不可见,被成员函数read_file调用
std::map<std::string, std::set<line_no> > word_map;
public:
std::set<line_no> run_query(const std::string & query_word);//查询单词所在的行号
std::string tex_line(line_no line) const;//显示某行文本内容
};
TextQuery.cpp
#include "TextQuery.h"
TextQuery::TextQuery()
{
}
TextQuery::~TextQuery()
{
}
void TextQuery::read_file(std::ifstream & is)
{
store_file(is);
build_map();
}
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.at(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)
{
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::tex_line(line_no line) const
{
if (line < lines_of_text.size())
return lines_of_text.at(line);
else
throw std::out_of_range("line number out of range");
}
main.cpp
#include "TextQuery.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
ifstream& open_file(ifstream &in, const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
string make_plural(size_t ctr, const string &word, const string &ending)
{
return (ctr == 1) ? word : word + ending;//处理字符串中单复数输出的一个函数,7.3.2中有原型
}
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) << ")" << file.tex_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;//这是C中的一种抛出错误的处理
}
TextQuery tq;
tq.read_file(infile);
while (true)
{
cout << "enter word to look for ,or q to quit:";
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;
}
makefile如下:
edit:main.o TextQuery.o
g++ -o edit main.o TextQuery.o
main.o:main.cpp TextQuery.h
g++ -c main.cpp
TextQuery.o:TextQuery.cpp TextQuery.h
g++ -c TextQuery.cpp
clean:
rm main.o TextQuery.o