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中的类开了个头.关于类的内容还有很多,今天就来搞一下类中的继承以及类的访问权限.说到类的继承,接触过 ...
随机推荐
- git 码云 使用记录
使用了码云的私有仓库. 一.首先下载安装git 安装完成后,在开始菜单里找到“Git”->“Git Bash”,蹦出一个类似命令行窗口的东西,就说明Git安装成功! 二.创建版本库 什么是版本库 ...
- word 或者 WPS 使用两个目录的时候去掉中间的空格间隙
在生成图表目录时,发现Office word图表目录中多个标题之间的空行无法删除,我是自己建的标签,比如“图1-”.“图2-”…….“表1-”.“表2-”…… 发现“图1-”.“图2-”…….“表1- ...
- 关于Hibernate的部分知识总结
[部分内容参考地址:https://www.cnblogs.com/woniu2219/p/7111857.html] Hibernate Hibernate是一个开放源代码的对象关系映射框架,它对J ...
- wordpress | 网站访问速度优化方案(Avada)
一.谷歌字体 原因: Wordpress系统默认使用谷歌字体,在国内谷歌域名被屏蔽,所以导致操作反应慢. 解决方法: 对于后台:找到Wordpress这个文件 /wp-includes/script- ...
- CRT7.3.1版本安装步骤
工具: Setup.exe安装程序 keygen.exe注册机 zwt.nfo 查看电脑信息(主要看自己电脑是x86还x64版本) 安装步骤(所有程序尽量以管理员身份启动) 1.安装SecureCRT ...
- 浅谈Django的中间件与Python的装饰器
浅谈Django的中间件 与Python的装饰器 一.原理 1.装饰器是Python的一种语法应用,利用闭包的原理去更改一个函数的功能,即让一个函数执行之前先到另外一个函数中执行其他需求语句,在执行该 ...
- [转]MySQL常用字符串函数
本文转载自:http://www.cnblogs.com/geaozhang/ 是最常用的的一种函数,在一个具体应用中通常会综合几个甚至几类函数来实现相应的应用: 1.LOWER(column|str ...
- PHP 通过命令异步执行PHP程序
通过PHP执行系统命令调用PHP执行程序,让进程挂起到后台执行,不影响用户页面交互. 控制器调用命令,不用等待,后台创建一个进程执行程序. system(“nohup php command.php ...
- 谷歌浏览器修改cookie访问网页的小插件——EditsThisCookie
cookie是服务器用来区分不同的浏览器客户端的,比如学生A和B同一时段用各自的电脑访问学校访问学校的教务系统查看成绩,登录之后,访问同一页面确出来不同的信息,并且不能查看对方的信息,这就是因为服务器 ...
- pip快速git项目安装
pip install git+https://github.com/xx/xx.git