C ++ Primer Chapter XII 12.3 of the standard library: text query exercise program

Exercise

12.27

This program has been made in the completion papers


class QueryResult {
	//懒得重复写,所以使用类型别名
	using str_vec_ptr = std::shared_ptr<vector<string>>;
	using str_set_map_ptr = std::shared_ptr<map<string, set<size_t>>>;
	using str_map_ptr = std::shared_ptr<map<string, size_t>>;
	friend ostream& print(ostream& out, const QueryResult& result);
public:
	QueryResult(string word,str_vec_ptr p1,str_set_map_ptr p2,str_map_ptr p3):query_word(word),text_content(p1),word_to_line_set_map(p2),word_count_map(p3) {

	}
private:
	//不使用类内初始化,使用TextQuery传入的参数进行初始化
	str_vec_ptr text_content;
	str_set_map_ptr word_to_line_set_map;
	str_map_ptr word_count_map;
	string query_word;
};

class TextQuery {
public:
	//默认有50行
	TextQuery(ifstream& ifile) {
		string word;
		while (std::getline(ifile, word)) {
			text_content->push_back(word);
		}
	};
	QueryResult query(const string&);
private:
	//因为需要共享数据,所以这些数据成员全部写成智能指针的形式
	//懒得在初始化列表中初始化参数了,直接使用类内初始化
	std::shared_ptr<vector<string>> text_content = std::make_shared<vector<string>>();
	std::shared_ptr<map<string, set<size_t>>> word_to_line_set_map = std::make_shared<map<string, set<size_t>>>();
	std::shared_ptr<map<string, size_t>> word_count_map = std::make_shared<map<string, size_t>>();
	
};

QueryResult TextQuery::query(const string& str) {

if ((*word_count_map).find(str)!= (*word_count_map).end()) {
		return QueryResult(str, text_content, word_to_line_set_map, word_count_map);
	}
	size_t line = 1;
	size_t word_count = 0;
	set<size_t> word_appear_line_set;
	for (const auto& line_str : *text_content) {
		string single_word;
		std::istringstream single_text_stream(line_str);
		while (single_text_stream >> single_word) {
			if (str == single_word) {
				word_appear_line_set.insert(line);
				//统计次数的加一
				++word_count;
				
			}
		}
		++line;
	}
	(*word_to_line_set_map)[str] = word_appear_line_set;
	(*word_count_map)[str] = word_count;
	return QueryResult(str,text_content,word_to_line_set_map,word_count_map);
}

ostream& print(ostream& out,const QueryResult& result) {
	string target_word = result.query_word;
	out << target_word << " appear " << (*result.word_count_map)[target_word] << " times" << endl;
	for (const auto & line : (*result.word_to_line_set_map)[target_word] ) {
		out << "line:" << line << " " << (*result.text_content)[line - 1]<<endl;
	}
	return out;
}

void runQueries(ifstream &infile) {
	TextQuery tq(infile);
	while (true) {
		cout << "输入你要查询的词" << endl;
		string s;
		if ((!(cin>>s)||s=="q")) {
			break;
		}
		print(cout,tq.query(s))<<endl;
	}
}

12.28

In fact, here is to split the contents of a document which is written above, I do not write here

12.29

Accustomed to using while, if use while in all the places you can use while or do_while, I do not consider while still do_while, and it can be done while working do_while, so I tend to use while.

12.30

Code has been written in a difference of 12.27 and the book is that the book is getting a text from a file, we have built a good index.

I was in the building when the index query, if the query again, the index already exists a direct result of the return, if it does not exist to build indexes

12.31

In my program, instead of using a vector set more trouble, because there are not allowed to repeat itself set of keywords, and the vector you need to judge for themselves

12.32

The QueryResult and TextQuery class std :: shared_ptr <vector> changed StrBlob on it, because it maintains the internal StrBolb a std :: shared_ptr <vector>, we can write StrBlob directly.

12.33

I am currently unclear
add members named begin and end, and returns an iterator to the position of a given query returns the row number of the set is , What does it mean?

If it is returned to the given query set that contains all the line numbers begin and end, it can be written as

set<size_t>::iterator begin() {
		return (*word_to_line_set_map)[query_word].begin();
	};
	set<size_t>::iterator end() {
		return (*word_to_line_set_map)[query_word].end();
	};

The get_file () returns a shared_ptr, pointing to the file QueryResult object. This phrase refers to all of the text if the vector comprises <string>, then can be written as follows

str_vec_ptr get_file() {
		return text_content;
	}
Published 54 original articles · won praise 6 · views 3305

Guess you like

Origin blog.csdn.net/zengqi12138/article/details/104417613