C++学习笔记之MyString的实现和迭代器

MyString类
迭代器的特点:遍历所有容器的方法都是一样的,通过begin和end方法实现

class CMyString
{
public:
	CMyString(const char*p = nullptr)
	{
		if (p != nullptr)
		{
			// 1.根据p指向得字符串得长度,开辟一块内存空间
			mptr = new char[strlen(p) + 1];
			// 2.把p指向得字符串拷贝到这块空间上来
			strcpy(mptr, p);
		}
		else
		{
			mptr = new char[1];
			*mptr = 0; // '\0'
		}
	}
	~CMyString()
	{
		delete []mptr;
		mptr = nullptr;
	}
	CMyString(const CMyString &str)
	{
		mptr = new char[strlen(str.mptr) + 1];
		strcpy(mptr, str.mptr);
	}
	CMyString(CMyString &&str)
	{
		mptr = str.mptr;
		str.mptr = nullptr;
	}
	// str1 = str2.operator=(str3);
	CMyString& operator=(const CMyString &str)
	{
		if (this == &str)
			return *this;

		delete[]mptr;

		mptr = new char[strlen(str.mptr) + 1];
		strcpy(mptr, str.mptr);
		return *this;
	}
	CMyString& operator=(CMyString &&str)
	{
		if (this == &str)
			return *this;

		delete[]mptr;

		mptr = str.mptr;
		str.mptr = nullptr;
		return *this;
	}

	bool operator>(const CMyString &str)const
	{
		return strcmp(mptr, str.mptr) > 0;
	}
	bool operator<(const CMyString &str)const
	{
		return strcmp(mptr, str.mptr) < 0;
	}
	bool operator==(const CMyString &str)const
	{
		return strcmp(mptr, str.mptr) == 0;
	}

	int length()const { return strlen(mptr); }
	char& operator[](int index) { return mptr[index]; }
	const char* c_str()const { return mptr; }



	// CMyString::iterator 
	**// 实现CMyString容器的迭代器**
	class iterator
	{
	public:
		// iterator()  operator!=   operator++() operator*
		iterator(char *p = nullptr) :_ptr(p) {}
		bool operator!=(const iterator &it)
		{
			return _ptr != it._ptr;
		}
		void operator++() { _ptr++; }
		char& operator*() { return *_ptr; }
		// char a = *it; *it='6';
	private:
		char *_ptr;
	};
	// 容器的begin和end方法,分别返回
	iterator begin() { return iterator(mptr); }
	iterator end() { return iterator(mptr + strlen(mptr)); }
	

private:
	char *mptr;

	friend ostream& operator<<(ostream &out, const CMyString &str);
	friend CMyString operator+(const CMyString &l, const CMyString &r);
};


CMyString operator+(const CMyString &l, const CMyString &r)
{
	int length = l.length() + r.length();
	char* p = new char[length + 1];
	strcpy(p, l.mptr);
	strcat(p, r.mptr);
	CMyString tmp;
	tmp.mptr = p;
	return tmp;
}
// CMyString str1 = str2 + str3;
// CMyString str1; str1= str2 + str3;
ostream& operator<<(ostream &out, const CMyString &str)
{
	out << str.mptr;
	return out;
}

STL六大组件:

1、容器(Containers):各种数据结构,如Vector,List,Deque,Set,Map,用来存放数据,STL容器是一种Class Template,就体积而言,这一部分很像冰山载海面的比率。
2、算法(Algorithms):各种常用算法如Sort,Search,Copy,Erase,从实现的角度来看,STL算法是一种Function Templates。
3、迭代器(Iterators):扮演容器与算法之间的胶合剂,是所谓的“泛型指针”,共有五种类型,以及其它衍生变化,从实现的角度来看,迭代器是一种将:Operators*,Operator,Operator++,Operator–等相关操作予以重载的Class Template。所有STL容器都附带有自己专属的迭代器——是的,只有容器设计者才知道如何遍历自己的元素,原生指针(Native pointer)也是一种迭代器。
4、仿函数(Functors):行为类似函数,可作为算法的某种策略(Policy),从实现的角度来看,仿函数是一种重载了Operator()的Class 或 Class Template。一般函数指针可视为狭义的仿函数。
5、配接器(适配器)(Adapters):一种用来修饰容器(Containers)或仿函数(Functors)或迭代器(Iterators)接口的东西,例如:STL提供的Queue和Stack,虽然看似容器,其实只能算是一种容器配接器,因为 它们的底部完全借助Deque,所有操作有底层的Deque供应。改变Functor接口者,称为Function Adapter;改变Container接口者,称为Container Adapter;改变Iterator接口者,称为Iterator
Adapter。配接器的实现技术很难一言蔽之,必须逐一分析。
6、分配器(Allocators):负责空间配置与管理,从实现的角度来看,配置器是一个实现了动态空间配置、空间管理、空间释放的Class Template。

                                                   -----------来自《STL源码剖析》
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值