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[]) ...
随机推荐
- word2vec初探
在自然语言处理入门里我们提到了词向量的概念,tf-idf的概念,并且在实际的影评正负面预测项目中使用了tf-idf,取得了还算不错的效果.这一篇,我们来尝试一下使用来自google的大名鼎鼎的word ...
- C# 实现Jwtbearer Authentication
Jwtbearer Authentication 什么是JWT JWT(JSON Web Token), 顾名思义就是在Web上以JSON格式传输的Token(RFC 7519). 该Token被设计 ...
- c#源码如何生成托管代码块
1.使用编程语言编写源码--->编程语言的编译器(面向Clr)---->生成IL代码和元数据(包含:代码中声名的类和成员 以及所引用的成员) 2.IL就被称之为托管代码,因为有Clr管理者 ...
- Prism 学习:从本地目录加载 Module
在 Prism 中,将外部模块加载到主程序有以下几种方式:Code.XAML.配置文件.指定模块目录:其中,如果要使用 Code 方式来加载 Module,则需要将该 Module 引用到当前项目中: ...
- 模拟获取post数据的方式
使用下面两种方法可以获取post数据 .通过$HTTP_RAW_POST_DATA获取 $post=$GLOBALS['HTTP_RAW_POST_DATA']; 但需要修改相应的php.ini指令 ...
- [leetcode]984. 不含 AAA 或 BBB 的字符串
给定两个整数 A 和 B,返回任意字符串 S,要求满足: S 的长度为 A + B,且正好包含 A 个 'a' 字母与 B 个 'b' 字母: 子串 'aaa' 没有出现在 S 中: 子串 'bbb' ...
- python中的魔法属性
目录 1. __doc__ 2. __module__ 和 __class__ 3. __init__ 4. __del__ 5. __call__ 6. __dict__ 7. __str__ 8. ...
- linux中make的用法
一.linux中make的用法 目的: 基本掌握了make 的用法,能在Linux系统上编程.环境: Linux系统准备: 准备三个文件:file1.c, file ...
- Vue项目build打包部署到Tomcat后,刷新报404错误解决方案
问题描述: 一.更新依赖,并打包项目 cd /root/.jenkins/workspace/v-test;npm installcd /root/.jenkins/workspace/v-test; ...
- gulp解决跨域的配置文件
//引入插件 var gulp = require('gulp'); // var Proxy = require('gulp-connect-proxy'); var connect = requi ...