c++ primer–容器的综合应用:文本查询程序
我们的程序将读取用户指定的任意文本文件,然后允许用户从该文件中查找单词。查询的结果是该单词出现的次数,并列出每次出现所在的行。如果某单词在同一行中多次出现,程序将只显示该行一次。行号按升序显示,即第 7 行应该在第 9 行之前输出,依此类推。
例如,以本章的内容作为文件输入,然后查找单词“element”。输出的前几行应为:
element occurs 125 times
(line 62) element with a given key.
(line 64) second element with the same key.
(line 153) element |==| operator.
(line 250) the element type.
(line 398) corresponding element.
后面省略了大约 120 行。
查询程序的设计
设计程序的一个良好习惯是首先将程序所涉及的操作列出来。明确需要提供的操作有助于建立需要的数据结构和实现这些行为。从需求出发,我们的程序需要支持如下任务:
1. 它必须允许用户指明要处理的文件名字。程序将存储该文件的内容,以便输出每个单词所在的原始行。
2. 它必须将每一行分解为各个单词,并记录每个单词所在的所有行。在输出行号时,应保证以升序输出,并且不重复。
3. 对特定单词的查询将返回出现该单词的所有行的行号。
4. 输出某单词所在的行文本时,程序必须能根据给定的行号从输入文件中获取相应的行。
数据结构
我们将用一个简单的类 TextQuery 实现这个程序。再加几种容器的配合使用,就可相当巧妙地满足上述要求。
- 使用一个 vector 类型的对象存储整个输入文件的副本。输入文件的每一行是该 vector对象的一个元素。因而,在希望输出某一行时,只需以行号为下标获取该行所在的元素即可。
- 将每个单词所在的行号存储在一个 set容器对象中(一个单词可能出现多次,所以用set)。使用 set 就可确保每行只有一个条目,而且行号将自动按升序排列。
- 使用一个 map 容器将每个单词与一个 set 容器对象关联起来,该 set 容器对象记录此单词所在的行号。
操作
对于类还要求有良好的接口。然而,一个重要的设计策略首先要确定:查询函数需返回存储一组行号的 set 对象。这个返回类型应该如何设计呢?
查询的过程相当简单:使用下标访问 map 对象获取关联的 set 对象即可。
唯一的问题是如何返回所找到的 set 对象。安全的设计方案是返回该 set 对象的副本。
如果处理的是一个相当庞大的文件,则复制 set对象的代价会非常昂贵。其他可行的方法包括:返回一个 pair 对象,存储一对指向 set 中元素的迭代器;或者返回 set 对象的 const 引用。
第一、第三和第四个任务是使用这个类的程序员将执行的动作。第二个任务则是类的内部任务。将这四任务映射为类的成员函数,则类的接口需提供下列三
个 public 函数:
• read_file 成员函数,其形参为一个 ifstream& 类型对象。该函数每次从文件中读入一行,并将它保存在 vector容器中。输入完毕后,read_file 将创建关联每个单词及其所在行号的map 容器。
• run_query 成员函数,其形参为一个 string 类型对象,返回一个 set 对象,该 set 对象包含出现该 string 对象的所有行的行号。
• text_line 成员函数,其形参为一个行号,返回输入文本中该行号对应的文本行。
无论 run_query 还是 text_line 都不会修改调用此函数的对象,因此,可将这两个操作定义为 const 成员函数。
为实现 read_file 功能,还需定义两个 private 函数来读取输入文本和创建 map 容器:
• store_file 函数读入文件,并将文件内容存储在 vector 容器对象中。
• build_map 函数将每一行分解为各个单词,创建 map 容器对象,同时记录每个单词出现行号。
TextQuery 类
class TextQuery {
public:
// typedef to make declarations easier
typedef std::vector<std::string>::size_type line_no;
/* interface:
* read_file builds internal data structures for the given file
* run_query finds the given word and returns set of lines on
which it appears
* text_line returns a requested line from the input file
*/
void read_file(std::ifstream &is)
{ store_file(is); build_map(); }
std::set<line_no> run_query(const std::string&) const;
std::string text_line(line_no) const;
private:
// utility functions used by read_file
void store_file(std::ifstream&); // store input file
void build_map(); // associated each word with a set of line numbers
// remember the whole input file
std::vector<std::string> lines_of_text;
// map word to set of the lines on which it occurs
std::map< std::string, std::set<line_no> > word_map;
};
这个类直接反映了我们的设计策略(类外调用的函数用public,类内调用的函数用private)。唯一提及的是使用 typedef 为 vector 的 size_type 定义了一个别名(行号line_no类型)。
read_file 函数在类的内部定义。该函数首先调用 store_file 读取并保存输入文件,然后调用 build_map 创建关联单词与行号的 map 容器。
TextQuery 类的使用
int main(int argc, char **argv)
{
// open the file from which user will query words
ifstream infile;
if (argc < 2 || !open_file(infile, argv[1])) {
cerr << "No input file!" << endl;
return EXIT_FAILURE;
}
TextQuery tq;
tq.read_file(infile); // builds query map
// iterate with the user: prompt for a word to find and print results
// loop indefinitely; the loop exit is inside the while
while (true) {
cout << "enter word to look for, or q to quit: ";
string s;
cin >> s;
// stop if hit eof on input or a 'q'is entered
if (!cin || s == "q") break;
// get the set of line numbers on which this word appears
set<TextQuery::line_no> locs = tq.run_query(s);
// print count and all occurrences, if any
print_results(locs, s, tq);
}
return 0;
}
输出结果:print_results 函数
void print_results(const set<TextQuery::line_no>& locs,
const string& sought, const TextQuery &file)
{
// if the word was found, then print count and all occurrences
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;
//make_plural根据 size 是否为 1 输出“time”或“times”。
// print each line in which the word appeared
line_nums::const_iterator it = locs.begin();
for ( ; it != locs.end(); ++it) {
cout << "\t(line "
// don't confound user with text lines start ing at 0
<< (*it) + 1 << ") "
<< file.text_line(*it) << endl;
}
}
编写成员函数
存储输入文件:store_file
第一个任务是读入需要查询的文件。使用 string 和 vector 容器提供的操作,可以很简便地实现这个任务:
// read input file: store each line as element in lines_of_text
void TextQuery::store_file(ifstream &is)
{
string textline;
while (getline(is, textline))
lines_of_text.push_back(textline);
}
函数从文件中每读入一行就将它添加到名为 lines_of_text 的 vector 对象中。
建立单词 map 容器
vector 容器中的每个元素就是一行文本。要建立一个从单词关联到行号的 map 容器,必须将每行分解为各个单词。再次使用第 8.5 节描述的 istringstream(字符串输入流):
// finds whitespace-separated words in the input vector
// and puts the word in word_map along with the line number
void TextQuery::build_map()
{
// process each line from the input vector
for (line_no line_num = 0;
line_num != lines_of_text.size();
++line_num)
{
//we'll use line to read the text a word at a time
istringstream line(lines_of_text[line_num]);
string word;
while (line >> word)
// add this line number to the set;
// subscript will add word to the map if it's not
already there
word_map[word].insert(line_num);
}
}
or 循环以每次一行的迭代过程遍历 lines_of_text。
首先将 istringstream 对象 line 与当前行绑定起来,然后使用 istringstream 的输入操作符读入该行中的每个单词。回顾此类输入操作符,与其他 istream 操作符一样,将忽略空白符号。因此,while 循环将 line 中以空白符分隔的单词读取出来。
这个函数的结尾部分类似前面的单词统计程序。将 word 用做 map 容器的下标。如果 word 在 word_map 容器对象中不存在,那么下标操作符将该 word 添加到此容器中,将其关联的值初始化为空的 set。不管是否添加了 word,下标运算都返回一个 set 对象,然后调用 insert 函数在该 set对象中添加当前行号。如果某个单词在同一行中重复出现,那么 insert 函数的调用将不做任何操作。
支持查询:run_query 函数
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);
//在map中查找,返回指向该key值的迭代器
if (loc == word_map.end())
return set<line_no>();
else
// fetch and return set of line numbers for this word
return loc->second; //loc->second即是map中的set对象
}
run_query 函数带有指向 const string 类型对象的引用参数,并以这个参数作为下标来访问 word_map对象。假设成功找到这个 string,那么该函数返回关联此 string 的 set 对象,否则返回一个空的 set 对象。
输出出现该单词的每一行:text_line
string TextQuery::text_line(line_no line) const
{
if (line < lines_of_text.size())
return lines_of_text[line];
throw std::out_of_range("line number out of range");
}
该函数带有一个行号参数,返回该行号所对应的输入文本行。应该首先检查我们要查询的行是否们于合法范围内。如果是,则返回相应的行,否则,抛出 out_of_range 异常。