设为首页 加入收藏

TOP

<三>自己实现string,加入迭代器功能(一)
2023-07-23 13:34:21 】 浏览:59
Tags:< > string 加入迭

迭代器的功能:
提供一种统一的方式,来透明的遍历容器
理解 begin()方法,end()方法, ++ , * 的用处
其中 C++11 中提供的foreach的方式,其底层还是通过迭代器来进行遍历的.

#include <iostream>
using namespace std;

class MyString2 {

public:

	//构造函数
	MyString2(const char * pSource = nullptr) {
		if (pSource != nullptr) {
			pString = new char[strlen(pSource) + 1];
			strcpy(pString, pSource);
		}
		else {
			pString = new char[1];
			pString[0] = '\0';
		}
		cout << "MyString构造函数,对象地址=" << this << endl;
	}

	//拷贝构造
	MyString2(const MyString2 & _rValue) {

		pString = new char[strlen(_rValue.pString) + 1];
		strcpy(pString, _rValue.pString);
		cout << "MyString拷贝构造函数" << endl;
	}

	//赋值函数
	MyString2 & operator=(const MyString2 & _rValue) {

		if (this == &_rValue) {
			return *this;
		}
		delete[] this->pString;
		this->pString = nullptr;

		char * _tpString = new char[strlen(_rValue.pString) + 1];
		strcpy(_tpString, _rValue.pString);

		cout << "MyString赋值函数" << endl;

	}

	//可编辑
	char & operator[](int index) {

		int len = strlen(this->pString);
		if (index<0) { return pString[0]; }
		else if (index>len) { return pString[index]; }
		else { return pString[index]; }

	}

	//不可编辑
	const char operator[](int index) const {

		int len = strlen(this->pString);
		if (index<0) { return pString[0]; }
		else if (index>len) { return pString[index]; }
		else { return pString[index]; }

	}

	bool operator>(const MyString2 & _rValue) const {
		return strcmp(this->pString, _rValue.pString)>0;
	}
	
	bool operator<(const MyString2 & _rValue) const {
		return strcmp(this->pString, _rValue.pString)<0;
	}
	
	bool operator==(const MyString2 & _rValue) const {
		return strcmp(this->pString, _rValue.pString) == 0;
	}

	int length() const {
		return strlen(this->pString);
	}

	~MyString2() {
		if (this->pString != nullptr) {
			delete[] this->pString;
			this->pString = nullptr;
			cout << "MyString析构函数" << this << endl;
		}
	}

	const char * c_str()  const { return this->pString; }

	//给Mystring 设计迭代器
	class Iterator {
		public:

			Iterator(char * _p=nullptr):p(_p) {
				this->p = _p;
			}
		    char & operator*() {
				return *p;
			}

			//前置++
			void operator++() {
				p++;
			}
			//
			bool operator!=(const Iterator & _val) {		
				return p!=_val.p;
			}
		private:

			char * p;
	};
	// begin() 返回的 用MyString的首元素地址,通过Iterator封装一下
	Iterator begin() const {
		return  Iterator(this->pString);
	}
	// end() 返回的是 用 MyString 的最尾元素的下一个地址 再用Iterator封装一下
    Iterator end() const {
		return  Iterator(this->pString+this->length());
	}

private:
	char * pString;
	friend MyString2 operator+  (const MyString2 & s1, const MyString2 &s2);
	friend ostream & operator<<(ostream & out, const MyString2 &s);
	
};

MyString2 operator+(const MyString2 & s1, const MyString2 &s2) {

	/*
	方式1 这段代码有 内存泄漏问题  tp 没有释放掉
	int newLength = strlen(s1.pString) + strlen(s2.pString) + 1;
	char *tp = new char[newLength + 1];//重新申请空间
	strcpy(tp, s1.pString);
	strcat(tp, s2.pString);

	MyString s(tp);
	cout << "operator+ = " << &s << endl;
	return s;
	*/

	/*
	方式2 对比方式1 效果更高
	*
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++ using 编译指令与名称冲突 下一篇OpenFoam——盖驱动空腔流动【算..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目