c++智能指针关联容器应用实例:文本查询程序


这是一个c++ primer 第五版的文本查询程序

<h1>对于学习/理解关联容器和智能指针 有一个很好的帮助。</h1>
</pre><pre name="code" class="cpp">// TextQuery.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <memory>
#include <map>
#include <unordered_map>
#include <set>
#include <string>
#include <sstream>


using namespace std;
using line_no = std::vector<std::string>::size_type;


inline
std::string make_plural(size_t ctr, const std::string &word, const std::string ending)
{
	return (ctr == 1) ? word : word + ending;
}
//类前声明
class QueryResult;

class TextQuery
{
public:
	//using line_no = std::vector<std::string>::size_type;
	TextQuery(std::ifstream&);
	QueryResult query(const std::string &) const;
private:
	//输入文件 使用一个智能指针进行保存文件指针 
	std::shared_ptr<std::vector<std::string>> file;  
	//每个单词到他所在的行号的集合的映射
	std::map<std::string, std::shared_ptr<std::set<line_no>>> wm;
	//std::unordered_map<std::string, std::shared_ptr<std::set<line_no>>> wm;
};

class QueryResult{
	friend std::ostream& print(std::ostream&, const QueryResult &);
public:
	QueryResult(std::string s,
				std::shared_ptr<std::set<line_no>> p,
				std::shared_ptr<std::vector<std::string>> f):
				sought(s), lines(p), file(f) {}
private:
	std::string sought;				//查找单词
	std::shared_ptr<std::set<line_no>> lines;	//出现的行号
	std::shared_ptr<std::vector<std::string>> file;	//输入文件
};

TextQuery::TextQuery(ifstream &is) : file(new vector<string>)//初始化为一个字符串容器类型的智能指针
{
	string text;
	while (getline(is, text))					//从文件中读取一行,存入text
	{
		file->push_back(text);					//保存此行内容到字符串容器中
		int n = file->size() - 1;               <span style="white-space:pre">		</span>//当前行号
		istringstream line(text);				//将行文本分解为单词
		string word;
		while (line >> word)					//读取每个单词
		{
									//如果单词不在wm中,已其为下标保存到wm
			auto &lines = wm[word];             <span style="white-space:pre">		</span>//lines 是一个shared_ptr (shared_ptr<set<line_no>>)
									//map使用一个不在容器中的关键字作为下标
									//会添加一个具有此关键字的元素到map中
			if (!lines)                         <span style="white-space:pre">		</span>//wm没有该单词,
				lines.reset(new set<line_no>); <span style="white-space:pre">		</span>//分配一个新的set
			lines->insert(n);				//将此行号插入到set中
		}
	}
}

//如果没有找到sought,将返回一个指向空的set指针
QueryResult TextQuery::query(const string &sought) const
{
	//定义一个指向一个空行号set的shared_ptr,当没有找到单词时,返回此对象的一份拷贝
	static shared_ptr<set<line_no>> nodata(new set<line_no>);
	//使用find而不是下标运算符来查找单词,避免将单词添加到wm中
	auto loc = wm.find(sought);
	if (loc == wm.end())
		return QueryResult(sought, nodata, file);//没找到
	else
		return QueryResult(sought, loc->second, file);
}

ostream & print(ostream &os, const QueryResult &qr)
{
	//如果找到了单词,打印出现的次数和所有出现的位置
	os << qr.sought << " occurs " << qr.lines->size() << " "
		<< make_plural(qr.lines->size(), "time", "s") << endl;
	//打印单词出现的每一行
	for (auto num : *qr.lines)
	//避免行号从0开始
	//使用指针偏移打印容器vector中第num个位置的元素
		os << "\t(line" << num + 1 << ") " << *(qr.file->begin() + num) << endl;
	return os;
}

int _tmain(int argc, _TCHAR* argv[])
{
	ifstream infile("test.txt");
	TextQuery tq(infile);
	while (true)
	{
		cout << "enter word to look for ,or q to quit:";
		string s;
		if (!(cin >> s) || s == "q")
			break;
		print(cout, tq.query(s)) << endl;
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值