c/c++ vector,map,set,智能指针,综合运用的小例子
标准库,智能指针,综合运用的小例子
功能说明:查询单词在文件中出现的次数,如果在同一行出现多次,只算一次。
比如查询单词:你好
输出的结果:
你好 出现了:2次
(行号 2)xxxxxxx 你好
(行号 3)bbb ccc 你好 xxxxx
注意点:代码的46行,必须使用引用。
//非常重要,必须用引用,要不然就会拷贝一个新的set给lines,不是map里的set
auto &lines = wm[word];//lines是shared_ptr
代码:
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <memory>
using namespace std;
class QueryResult{
friend ostream& print(ostream&, const QueryResult&);
public:
using line_no = vector<string>::size_type;
QueryResult(string s, shared_ptr<set<line_no>> p,
shared_ptr<vector<string>> f):
sought(s), lines(p), file(f){}
private:
string sought;//查询的单词
shared_ptr<set<line_no>> lines;//出现的行号
shared_ptr<vector<string>> file;
};
//QueryResult的友元函数
ostream& print(ostream& os, const QueryResult& qr){
os << qr.sought << " 出现了:" << qr.lines->size() << "次" << endl;
for(auto num : *qr.lines){
os << "\t(行号 " << num + 1 << ")"
<< *(qr.file->cbegin() + num) << endl;
}
return os;
}
class TextQuery{
public:
using line_no = vector<string>::size_type;
TextQuery(ifstream& is) : file(new vector<string>){
string text;
while(getline(is, text)){//读文件的每一行
file->push_back(text);
int n = file->size() - 1;//当前行号
istringstream line(text);//将行文本分解为单词
string word;
while(line >> word){
//非常重要,必须用引用,要不然就会拷贝一个新的set给lines,不是原来的
auto &lines = wm[word];//lines是shared_ptr
if(!lines)
lines.reset(new set<line_no>);
lines->insert(n);
}
}
}
QueryResult query(const string &sought) const{
//如果没有找到sought,返回指向此set的一个智能指针
static shared_ptr<set<line_no>> nodata(new set<line_no>);
auto ret = wm.find(sought);
if(ret == wm.end()){
return QueryResult(sought, nodata, file);//没有找到
}
else{
return QueryResult(sought, ret->second, file);
}
}
private:
shared_ptr<vector<string>> file;
map<string, shared_ptr<set<line_no>>> wm;
};
int main(){
ifstream infile("/home/ys/c++_template/search_text");
TextQuery tq(infile);
while(true){
cout << "输入要查找的单词: q 退出";
string s;
if(!(cin >> s) || s == "q")break;
print(cout, tq.query(s)) << endl;;
}
}
c/c++ 学习互助QQ群:877684253
本人微信:xiaoshitou5854
c/c++ vector,map,set,智能指针,综合运用的小例子的更多相关文章
- c++学习笔记—动态内存与智能指针浅析
我们的程序使用内存包含以下几种: 静态内存用来保存局部static对象.类static数据成员以及定义在任何函数之外的变量,在使用之前分配,在程序结束时销毁. 栈内存用来保存定义在函数内部的非stat ...
- c++ auto_ptr智能指针
c++ auto_ptr智能指针 该类型在头文件memory中,在程序的开通通过 #include<memory> 导入,接下来讲解该智能指针的作用和使用. 使用方法: auto_ptr& ...
- c/c++ 继承与多态 文本查询的小例子(非智能指针版本)
问题:在上一篇继承与多态 文本查询的小例子(智能指针版本)在Query类里使用的是智能指针,只把智能指针换成普通的指针,并不添加拷贝构造方法,会发生什么呢? 执行时,代码崩掉. 分析下面一行代码: Q ...
- c/c++ 继承与多态 文本查询的小例子(智能指针版本)
为了更好的理解继承和多态,做一个文本查询的小例子. 接口类:Query有2个方法. eval:查询,返回查询结果类QueryResult rep:得到要查询的文本 客户端程序的使用方法: //查询包含 ...
- c++智能指针(1)
根据muduo开源库作者陈硕的一些文章.对于多线程下C++编程提出了一些观点.主要是多线程下对象的销毁比较困难,但是由于多线程下,mutext是无法保护析构的.而后提出了智能指针的方案并对使用该指针会 ...
- c++11 智能指针 unique_ptr、shared_ptr与weak_ptr
c++11 智能指针 unique_ptr.shared_ptr与weak_ptr C++11中有unique_ptr.shared_ptr与weak_ptr等智能指针(smart pointer), ...
- C++:(拷贝,继承,智能指针)练习
#include <iostream> #include <string> #include <memory> #include <functional> ...
- C++ 基础知识回顾(string基础、智能指针、迭代器、容器类)
[1] string基础 [1.1] string 的构造 #include <iostream> #include <string> int main() { using n ...
- c++ boost库学习二:内存管理->智能指针
写过C++的人都知道申请和释放内存组合new/delete,但同时很多人也会在写程序的时候忘记释放内存导致内存泄漏.如下所示: int _tmain(int argc, _TCHAR* argv[]) ...
随机推荐
- 详解CSS的Flex布局
本文由云+社区发表 Flex是Flexible Box 的缩写,意为"弹性布局",是CSS3的一种布局模式.通过Flex布局,可以很优雅地解决很多CSS布局的问题.下面会分别介绍容 ...
- Go Web:数据存储(1)——内存存储
数据可以存储在内存中.文件中.按二进制序列化存储的文件中.数据库中等. 1.内存存储 2.CSV文件存储 3.gob序列化存储 内存存储 将数据存储到内存中.此处所指的内存是指应用程序自身的内存空间( ...
- springMVC中的注解@RequestParam与@PathVariable的区别
1.@PathVariable @PathVariable绑定URI模板变量值 @PathVariable是用来获得请求url中的动态参数的 @PathVariable用于将请求URL中的模板变量映射 ...
- zepto 事件分析4(事件队列)
前面分析了zepto的事件绑定,接下来分析事件解绑,先看一下zepto中解绑的off方法: $.fn.off = function(event, selector, callback){ var $t ...
- [转]js串口通信 调用MSCOMM32控件 链接电子秤
本文转自:https://www.cnblogs.com/x-j-p/p/7819724.html 硬件环境:RS232转USB串口线*1 电子秤*1(本人采用G&G E600Y-C型号称重仪 ...
- 第一册:lesson twenty seven。
原文 :Mrs.smith's living room. Mrs.smith's living room is large. There is a television in the room. Th ...
- 第一册:lesson seventeen。
原文:How do ;you do? A:Come and meet our employees,Mr.B. B:Thank you Mr.A. A:This is ...and this is .. ...
- “每日一道面试题”.Net中所有类的基类是以及包含的方法
闲来无事,每日一贴.水平有限,大牛勿喷. .Net中所有内建类型的基类是System.Object毋庸置疑 Puclic Class A{}和 Public Class A:System.Object ...
- springboot用户登陆密码两次md5加密
1.用户端:PASS = MD5(明文 + 固定salt) 2.服务端:PASS = MD5(用户输入 + 随机salt) 引入依赖包 <dependency> <groupId&g ...
- 百度前端学院-基础学院-第七天到第八天之BFC
1.BFC简介 BFC全称"block formatting context",中文为“块级格式化上下文”.特征总之记住一句话: BFC元素特征表现原则就是:内部元素无论怎么翻江倒 ...