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中的类开了个头.关于类的内容还有很多,今天就来搞一下类中的继承以及类的访问权限.说到类的继承,接触过 ...
随机推荐
- 纯JS实现前端动态分页码
思路分析:有3种情况 第一种情况,当前页面curPage < 4 第二种情况,当前页面curPage == 4 第三种情况,当前页面curPage>4 此外,还要考虑,当前页码 curPa ...
- js滚动监听
下边代码,是监听滚动条只要移动,下方的返回顶部的div显示与隐藏的代码 ? 1 2 3 4 5 6 7 8 window.onscroll = function () { var t = docum ...
- jQuery表单验证的几种方法
1.jQuery的框架的验证:validate框架 Jquery Validate 验证规则 (1)required:true 必输字段(2)remote:”check.PHP” 使用ajax方法调用 ...
- sample采样倾斜key并单独进行join代码
/** * sample采样倾斜key单独进行join */ JavaPairRDD<Long, String> sampledRDD = userid2PartAggrInfoRDD.s ...
- pymyspl模块
pymysql的下载和使用 该模块本质就是一个套接字客户端软件,使用前需要事先安装,能够让我们在 Python程序中操作数据库. pymysql模块的下载: 在Python安装文件中找到scripts ...
- react路由按需加载方法
使用router4之后以前的按需加载方法require.ensure 是不好使了. 所以我们改用react-loadable插件做按需加载. 第一步: yarn add react-loadable ...
- mybatis入门(一):jdbc的缺点
mybatis的基础内容 1.mybatis的框架原理 2.mybatis开发dao两种方法: a.原始dao开发方法(程序需要编写dao接口和dao实现类) b.mybatis的mapper接口(相 ...
- 免考final linux提权与渗透入门——Exploit-Exercise Nebula学习与实践
免考final linux提权与渗透入门--Exploit-Exercise Nebula学习与实践 0x0 前言 Exploit-Exercise是一系列学习linux下渗透的虚拟环境,官网是htt ...
- 成都优步uber司机第一组与第二组的区别
成都优步uber司机被分成了两组,两组的奖励方式不相同,下面我们先来看看官方给出的奖励方式: 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注册Uber司机(全国版最 ...
- 北京Uber优步司机奖励政策(1月27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...