C++简易string类的实现(包含迭代器遍历数据)

本文详细介绍了如何在C++中实现MyString类的构造、赋值、访问、比较、运算符重载等功能,包括字符串长度计算、空判断、迭代器使用等,并通过VS2019 IDE演示了实际操作和测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写了3个小时才完成,运算符重载还得多练习

开发工具:宇宙第一ide VS2019!

废话不多说,直接上代码!

File: MyString.h

#pragma once
#include <iostream>

class MyString
{
public:	// 构造与析构
	MyString(const char* str = "");
	MyString(const MyString& str);
	~MyString();

public:	
	// 访问属性的函数
	int length();		// 返回存储字符串的长度
	char* c_str();		// 返回存储的字符串
	char* data();		// 返回存储的字符串
	// 判断空
	bool isEmpty();		// 判断是否为空
	bool isNotEmpty();	// 判断是否不为空

protected:	// 保护属性
	char* pStr;
	int strLen;

public:	// 重载运算符
	// 赋值
	void operator=(MyString str);
	void operator=(const char* str);

	// 元素、地址访问
	char operator[](int sub);
	char* operator+(int sub);

	// 比较
	bool operator==(MyString str);
	bool operator!=(MyString str);
	bool operator>(MyString str);
	bool operator<(MyString str);
	bool operator>=(MyString str);
	bool operator<=(MyString str);

	// 非(判断是否为空,空返回true)
	bool operator!();

	// 连接
	friend MyString operator+(MyString str1, MyString str2);
	friend MyString operator+(MyString str1, const char* str2);
	friend MyString operator+(const char* str1, MyString str2);
	friend void operator+=(MyString& str1, MyString str2);
	friend void operator+=(MyString& str1, const char* str2);

	// 重复
	friend MyString operator*(MyString str, int multiplier);
	friend MyString operator*(int multiplier, MyString str);
	friend void operator*=(MyString& str, int multiplier);	

	// 流重载
	friend std::istream& operator>>(std::istream& in, MyString& str);
	friend std::ostream& operator<<(std::ostream& out, MyString& str);


public:	// 迭代器
	class Iterator
	{
	public:
		Iterator()
		{
			this->pmstr = nullptr;
			this->curSub = 0;
		}
		Iterator(MyString* pmstr, int sub = 0)
		{
			this->pmstr = pmstr;
			this->curSub = sub;
		}

	protected:
		const MyString* pmstr;
		int curSub;

	public:
		void operator=(Iterator iterator)
		{
			this->pmstr = iterator.pmstr;
			this->curSub = iterator.curSub;
		}
		bool operator!=(Iterator iterator)
		{
			if (this->pmstr != iterator.pmstr || this->curSub != iterator.curSub)
				return true;
			else
				return false;
		}
		void operator++()
		{
			this->curSub++;
		}
		void operator++(int)
		{
			++this->curSub;
		}
		char operator*()
		{
			return *(this->pmstr->pStr + this->curSub);
		}
	};

	Iterator begin()
	{
		return Iterator(this, 0);
	}
	Iterator end()
	{
		return Iterator(this, this->strLen);
	}
};

File: MyString.cpp

#include "MyString.h"
#include <cstring>
#include <crtdbg.h>	// 断言

MyString::MyString(const char* str)
{
	this->strLen = strlen(str);
	this->pStr = new char[this->strLen + 1];
	strcpy_s(this->pStr,this->strLen + 1, str);
}

// 深拷贝构造
MyString::MyString(const MyString& str)
{
	this->strLen = str.strLen;
	this->pStr = new char[this->strLen + 1];
	strcpy_s(this->pStr, this->strLen + 1, str.pStr);
}

MyString::~MyString()
{
	delete[] this->pStr;
	this->pStr = nullptr;
}

int MyString::length()
{
	return this->strLen;
}

char* MyString::c_str()
{
	return this->pStr;
}

char* MyString::data()
{
	return this->pStr;
}

bool MyString::isEmpty()
{
	return (strcmp(this->pStr, "") == 0);
}

bool MyString::isNotEmpty()
{
	return (strcmp(this->pStr, "") != 0);
}

void MyString::operator=(MyString str)
{
	this->strLen = str.strLen;
	delete[] this->pStr;
	this->pStr = new char[this->strLen + 1];
	strcpy_s(this->pStr, this->strLen + 1, str.pStr);
}

void MyString::operator=(const char* str)
{
	this->strLen = strlen(str);
	delete[] this->pStr;
	this->pStr = new char[this->strLen + 1];
	strcpy_s(this->pStr, this->strLen + 1, str);
}

char MyString::operator[](int sub)
{
	if (sub < 0 || sub >= this->strLen)
		_ASSERT_EXPR(false, L"String's Subscript Out of Range!");
	else
		return this->pStr[sub];
}

char* MyString::operator+(int sub)
{
	if (sub < 0 || sub >= this->strLen)
		_ASSERT_EXPR(false, L"String's Subscript Out of Range!");
	else
		return this->pStr + sub;
}

bool MyString::operator==(MyString str)
{
	return (strcmp(this->pStr, str.pStr) == 0);
}

bool MyString::operator!=(MyString str)
{
	return (strcmp(this->pStr, str.pStr) != 0);
}

bool MyString::operator>(MyString str)
{
	return (strcmp(this->pStr, str.pStr) > 0);
}

bool MyString::operator<(MyString str)
{
	return (strcmp(this->pStr, str.pStr) < 0);
}

bool MyString::operator>=(MyString str)
{
	return (strcmp(this->pStr, str.pStr) >= 0);
}

bool MyString::operator<=(MyString str)
{
	return (strcmp(this->pStr, str.pStr) <= 0);
}

bool MyString::operator!()
{
	return (strcmp(this->pStr, "") == 0);
}

MyString operator+(MyString str1, MyString str2)
{
	int size = str1.strLen + str2.strLen + 1;
	char* returnStr = new char[size];
	strcpy_s(returnStr, size, str1.pStr);
	strcat_s(returnStr, size, str2.pStr);
	return MyString(returnStr);
}

MyString operator+(MyString str1, const char* str2)
{
	MyString tmp(str2);
	return str1 + tmp;
}

MyString operator+(const char* str1, MyString str2)
{
	MyString tmp(str1);
	return tmp + str2;
}

void operator+=(MyString& str1, MyString str2)
{
	str1 = str1 + str2;
}

void operator+=(MyString& str1, const char* str2)
{
	str1 = str1 + str2;
}

MyString operator*(MyString str, int multiplier)
{
	// 乘数小于1返回空字符串(大于0小于1会转换为0)
	if(multiplier < 1)
		return MyString();
	else
	{
		MyString tmp(str);
		for (int i = 0; i < multiplier - 1; i++)
			str += tmp;
		return str;
	}
}

MyString operator*(int multiplier, MyString str)
{
	return str * multiplier;
}

void operator*=(MyString& str, int multiplier)
{
	str = str * multiplier;
}

std::istream& operator>>(std::istream& in, MyString& str)
{
	char input[1024] = { 0 };
	in >> input;
	str = MyString(input);
	return in;
}

std::ostream& operator<<(std::ostream& out, MyString& str)
{
	out << str.pStr;
	return out;
}

测试程序 File: test.cpp

#include "MyString.h"
#include <iostream>
using namespace std;

int main()
{
	MyString str("I Love You!");

	// 重载[]测试
	for (int i = 0; i < str.length(); i++)
		cout << str[i];
	cout << endl;

	// 重载<<测试
	cout << str << endl;

	// 拷贝构造测试
	MyString str2(str);
	cout << str2 << endl;

	// 一些连接的测试
	str2 = str + "I Miss You!";
	cout << str2 << endl;
	str2 = str + str;
	cout << str2 << endl;
	str2 += "Hah";
	cout << str2 << endl;

	str2 *= 4;
	cout << str2 << endl;


	// 迭代器测试
	MyString::Iterator it;
	for (it = str2.begin(); it != str2.end(); it++)
		cout << *it;
	cout << endl;

	

	return 0;
}

运行结果(测试全部通过!)

 

 时候不早了,睡觉

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值