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);
	}
}
相关推荐
光影少年17 分钟前
AI Agent、自动化与未来工作
人工智能·算法·架构
爱吃香菜的初学者18 分钟前
十六.Linux——多线程(2)
linux·运维·开发语言
牧羊人.33327 分钟前
计算机视觉基础 第16章|EigenFace,FisherFace,LBPH传统人脸识别算法
人工智能·算法·计算机视觉
jeo陽32 分钟前
04-循环实战:素数、最大公约数、级数求和与枚举法
c语言·开发语言·数据结构
Nil20839 分钟前
leetcode 72编辑距离
算法·leetcode·职场和发展
划破黑暗的第一缕曙光1 小时前
[C++]:2.类和对象(下)
c++·类和对象·对象·类
暖核1 小时前
kubeadm 集群运维:Worker 节点下线删除、整套集群销毁重建指南
运维·开发语言·php
神工坊1 小时前
技术分享︱SIMFORGE HSF® 网格加密与热点加速技术实践
算法·hpc·网格·cae·工程仿真·数值引擎·samr
CV@CV1 小时前
世界模型与智能体协同推理逻辑
人工智能·算法·边缘计算·量子计算·具身智能
jiayoushijie-泽宣1 小时前
SHIFT-PathFormer:专家轨迹蒸馏、连续安全优化与超大迷宫实验
算法