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);
	}
}
相关推荐
郭涤生1 天前
布隆过滤器
c++
喵了meme1 天前
C语言实战4
c语言·开发语言
码界奇点1 天前
Python从0到100一站式学习路线图与实战指南
开发语言·python·学习·青少年编程·贴图
智者知已应修善业1 天前
【求中位数】2024-1-23
c语言·c++·经验分享·笔记·算法
9ilk1 天前
【C++】--- 特殊类设计
开发语言·c++·后端
地平线开发者1 天前
PTQ 量化数值范围与优化
算法·自动驾驶
sali-tec1 天前
C# 基于halcon的视觉工作流-章68 深度学习-对象检测
开发语言·算法·计算机视觉·重构·c#
测试人社区-小明1 天前
智能弹性伸缩算法在测试环境中的实践与验证
人工智能·测试工具·算法·机器学习·金融·机器人·量子计算
罗西的思考1 天前
【Agent】MemOS 源码笔记---(5)---记忆分类
人工智能·深度学习·算法
生骨大头菜1 天前
使用python实现相似图片搜索功能,并接入springcloud
开发语言·python·spring cloud·微服务