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中的类开了个头.关于类的内容还有很多,今天就来搞一下类中的继承以及类的访问权限.说到类的继承,接触过 ...
随机推荐
- eclipse官方下载地址
下载地址: https://www.eclipse.org/downloads/packages/release/Luna/SR2 选择不同版本
- Linux 只显示目录或者文件方法
ls 参数 -a 表示显示所有文件,包含隐藏文件-d 表示显示目录自身的属性,而不是目录中的内容-F 选项会在显示目录条目时,在目录后加一个/ 只显示目录 方法一: find . -type d -m ...
- ubuntu 防止软件包自动更新
阻止软件包升级 有两种方法阻止软件包升级,使用dpkg,或者在Woody中使用APT. 使用dpkg,首先导出软件包选择列表: dpkg --get-selections \* > select ...
- QOS-policy配置
QOS-QOS-policy配置 2018年7月7日 20:29 配置: 先匹配acl: [RT2]acl number 3000 [RT2-acl-adv-3000]description QOS ...
- 中国大学MOOC-C程序设计(浙大翁恺)—— 时间换算
时间换算(10分) 题目内容: UTC是世界协调时,BJT是北京时间,UTC时间相当于BJT减去8.现在,你的程序要读入一个整数,表示BJT的时和分.整数的个位和十位表示分,百位和千位表示小时.如果小 ...
- pix2code开发笔记
1.软件安装 首先需要安装Python3和pip (1) Python3 环境搭建 Window 平台安装 Python: https://www.python.org/downloads/wind ...
- 成都Uber优步司机奖励政策(1月14日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- c++ 重载运算与类型转换
1. 基础概念 重载的运算符是具有特殊名字的函数:(重载运算符函数,运算符函数.重载运算符) 依次包含返回类型,函数名(operator=),参数列表,函数体. 只有重载的函数调用运算符operato ...
- spring源码-bean之加载-2
一.前面说了bean的容器初始化,后面当然是说bean的加载.这里还是不讲解ApplicationContext的bean的加载过程,还是通过最基础的XmlBeanFactory来进行讲解,主要是熟悉 ...
- Http接口系列:如何提高Http接口用例的数据稳定性
此文已由作者王婷英授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 为了尽可能多的释放手工测试,提高测试效率,我们都会想到使用自动化测试,如http接口自动化测试.doubbo ...