C++ //练习 12.2 编写你自己的StrBlob类,包含const版本的front和back。

C++ Primer(第5版) 练习 12.2

练习 12.2 编写你自己的StrBlob类,包含const版本的front和back。

环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp 复制代码
class StrBlob{
public:
	typedef vector<string>::size_type 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 &t) { data->push_back(t); }
	void pop_back();
	string &front();
	string &back();
	string &front() const;
	string &back() const;
private:
	shared_ptr<vector<string>> data;
	void check(size_type i, const string &msg) const;
};

void StrBlob::pop_back(){
	check(0, "pop_back on empty StrBlob");
	data->pop_back();
}

string& StrBlob::front(){
	check(0, "front on empty StrBlob");
	return data->front();
}

string& StrBlob::back(){
	check(0, "back on empty StrBlob");
	return data->back();
}

string& StrBlob::front() const{
	check(0, "front on empty StrBlob");
	return data->front();
}

string& StrBlob::back() const{
	check(0, "back on empty StrBlob");
	return data->back();
}

void StrBlob::check(size_type i, const string &msg) const{
	if(i >= data->size()){
		throw out_of_range(msg);
	}
}
相关推荐
jimy115 分钟前
readelf 查看“派生类”的vtable符号
开发语言·c++
蒸蒸yyyyzwd40 分钟前
cpp 选手秋招学习笔记 day37 面经学习
c++·八股
ctlover1 小时前
排序与查找算法详解
开发语言·python
YYYing.1 小时前
【C++进阶系列 (七)】C++ RTTI 深度剖析:从 typeid 到 dynamic_cast 的底层之旅
开发语言·c++·c/c++·rtti
shirsl1 小时前
算法 Day 5 树 / 二叉树 + DFS
数据结构·python·算法
木子算法2 小时前
非凸、离散、还耦合:论文里的求解方法是一条四步流水线
人工智能·算法·目标跟踪
虚无的纽扣2 小时前
【力扣刷题】第二天:无重复字符的最长字串、移动零问题
算法·leetcode·排序算法
张祥6422889042 小时前
牛顿迭代法求解开普勒方程:从RTKLIB源码到数值分析
人工智能·算法·机器学习
Niuguangshuo2 小时前
论文解读:Deep Speech 2,工业级英中端到端 ASR 系统报告
算法·音视频·语音识别
nvvas2 小时前
Java 入门(四):数组——数据的集合管理
java·开发语言