StrBlobPtr类——weak_ptr访问vector元素
#include <iostream>
#include <memory>
#include <string>
#include <initializer_list>
#include <vector>
#include <stdexcept> using namespace std; class StrBlobPtr; class StrBlob {
friend class StrBlobPtr;
public:
using size_type = vector<string>::size_type;
StrBlob();
StrBlob(initializer_list<string> il);
size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
void push_back(const string &s);
void pop_back();
//返回string的引用,是因为调用点会使用该string
//如b.front() = "first";
string& front();
string& back();
//只有const StrBlob对象才会调用以下函数
const string& front() const;
const string& back() const;
StrBlobPtr begin();
StrBlobPtr end();
private:
shared_ptr<vector<string>> data;
void check(size_type i, const string &msg) const;
}; StrBlob::StrBlob(): data(make_shared<vector<string>>())
{
} StrBlob::StrBlob(initializer_list<string> il): data(make_shared<vector<string>>(il))
{
} void StrBlob::check(size_type i, const string &msg) const
{
if (i >= data->size())
throw out_of_range(msg);
} void StrBlob::push_back(const string &s)
{
data->push_back(s);
} void StrBlob::pop_back()
{
check(0, "此StrBlob对象指向一个空vector!\n");
data->pop_back();
} string& StrBlob::front()
{
check(0, "此StrBlob对象指向一个空vector!\n");
return data->front();
} string& StrBlob::back()
{
check(0, "此StrBlob对象指向一个空vector!\n");
return data->back();
} const string& StrBlob::front() const
{
check(0, "此StrBlob对象指向一个空vector!\n");
cout << "调用对象为const StrBlob!\n";
return data->front();
} const string& StrBlob::back() const
{
check(0, "此StrBlob对象指向一个空vector!\n");
cout << "调用对象为const StrBlob!\n";
return data->back();
} class StrBlobPtr {
public:
StrBlobPtr(): curr(0) {}
StrBlobPtr(StrBlob &b, size_t sz = 0): wptr(b.data), curr(sz) {}
string& deref() const;
StrBlobPtr& incr();
private:
weak_ptr<vector<string>> wptr;
size_t curr;
shared_ptr<vector<string>> check(size_t i, const string &msg) const;
}; shared_ptr<vector<string>> StrBlobPtr::check(size_t i, const string &msg) const
{
auto ret = wptr.lock();
if (!ret)
throw runtime_error("要访问的vector<string>对象不存在!\n");
if (i >= ret->size())
throw out_of_range(msg);
return ret;
} string& StrBlobPtr::deref() const
{
auto p = check(curr, "当前下标不合法!\n");
return (*p)[curr];
} StrBlobPtr& StrBlobPtr::incr()
{
check(curr, "不能继续递增了\n");
++curr;
return *this;
} StrBlobPtr StrBlob::begin()
{
return StrBlobPtr(*this);
} StrBlobPtr StrBlob::end()
{
return StrBlobPtr(*this, data->size());
} int main()
{
StrBlob b1{"mon", "tue", "wed", "thu", "fri"};
StrBlobPtr p(b1, 3);
cout << p.deref() << endl; //访问p当前指向的元素
cout << p.incr().deref() << endl; //先递增p,再访问元素
p = b1.begin();
cout << p.deref() << endl;
return 0;
}
StrBlobPtr类——weak_ptr访问vector元素的更多相关文章
- 访问vector元素方法的效率比较(转)
LInux下: gcc 4.47,red hat6 #include<iostream> #include<vector> #include<time.h> usi ...
- Struts2 访问web元素
访问web元素的四种方法(耦合,依赖注入).(耦合,非依赖注入).(非耦合,依赖注入).(非耦合,非依赖注入) 耦合:可以得到HttpServletResponse,HttpServletReques ...
- c++ vector(向量)使用方法详解(顺序访问vector的多种方式)
来源:http://www.jb51.net/article/44231.htm 作者: 字体:[增加 减小] 类型:转载 时间:2013-12-08我要评论 vector是向量类型,它可以容纳许多类 ...
- Struts2学习---简单的数据校验、访问Web元素
1.简单的数据校验 在action里面我们已经给出了一个数据校验: public String execute() { if(user.getUsername().equals("usern ...
- 413 重温HTML + css 考试 + 访问HTML元素
考试前的复习 初学css1:认识CSS 1.1:css简介,css全称是层叠样式表,Cascading style sheets 1.2:css的作用,主要是用于定义html内容在浏览器内的显示样式, ...
- 关于伪类“:pseudo-class”和伪元素“::pseudo-element”的常见应用
伪类用于指定要选择的元素的特殊状态,向其添加特殊的效果,比如: input { width: 515px; height: 50px; padding: 10px 20px; border: 1px ...
- 01_12_Struts2_访问Web元素
01_12_Struts2_访问Web元素 1. 配置struts.xml文件 <package name="login" namespace="/login&qu ...
- C++中类继承public,protected和private关键字作用详解及派生类的访问权限
注意:本文有时候会用Visual Studio Code里插件的自动补全功能来展示访问权限的范围(当且仅当自动补全范围等价于对象访问权限范围的时候),但是不代表只要是出现在自动补全范围内的可调用对象/ ...
- 窥探Swift之类的继承与类的访问权限
上一篇博客<窥探Swift之别具一格的Struct和Class>的博客可谓是给Swift中的类开了个头.关于类的内容还有很多,今天就来搞一下类中的继承以及类的访问权限.说到类的继承,接触过 ...
随机推荐
- 创建<Bean>sessionFactory错误, init方法调用失败;嵌套异常是org.hibernate.exception。
未知原因:在Maven中hibernate映射开启了自动更新表,出现此异常 org.springframework.beans.factory.BeanCreationException: Error ...
- window下使用Composer安装yii2
1.在 Windows 中,先下载并运行 Composer-Setup.exe,安装过程需选择php的运行目录,安装完后在windows的cmd下运行composer看看是否安装成功 2.安装完Com ...
- Python实现音乐的剪辑
一.读取音频文件 from scipy.io import wavfile import numpy as np like = wavfile.read('./嘤嘤嘤.wav') print (lik ...
- windows 10 安装node.js
第一步:下载软件 nodejs的中文官网http://nodejs.cn/download/ 选择 windows 系统 msi 安装版本. 下载完成之后,直接打开下一步安装就可以. 安装完成 打开 ...
- Product Helper
using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; /// <summary> /// 产品 ...
- AtCoder Regular Contest 100 E - Or Plus Max
一道很好的dp题 dp[K]存的是 i满足二进制1属于K二进制1位置 最大的两个Ai 这样dp[K]统计的两个数肯定满足(i | j) <= K 然后不断做 update(dp[i | (1&l ...
- 【blockly教程】第五章 循环结构
在这里,我们将介绍一个新游戏--Pond Tutor 在Pond Tutor(https://blockly-games.appspot.com/pond-tutor)这个游戏中,我们将扮演黄色的鸭子 ...
- 使用 Vim 搭建 JavaScript 开发环境
原文链接: https://spacevim.org/cn/use-vim-as-a-javascript-ide/ SpaceVim 是一个模块化的 Vim IDE,针对 JavaScript 这一 ...
- POJ2431_Expedition_KEY
题目传送门 由题目可得,在一条路上有N个加油站,在距离终点a[i](细节)的位置上,你需要通过长度为L的路,油箱的容量是无限的,但是初始只有P点油,经过每个加油站时可以选择加b[i]的油,问最少加油几 ...
- Python之数据处理
一.CSV数据处理 CSV文件格式:逗号分隔值(Comma-Separated Value,CSV,有时也称为字符分隔值,因为分隔符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本).纯文 ...