IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发工具 -> C++简易string类的实现(包含迭代器遍历数据) -> 正文阅读

[开发工具]C++简易string类的实现(包含迭代器遍历数据)

写了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;
}

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

?

?时候不早了,睡觉

  开发工具 最新文章
Postman接口测试之Mock快速入门
ASCII码空格替换查表_最全ASCII码对照表0-2
如何使用 ssh 建立 socks 代理
Typora配合PicGo阿里云图床配置
SoapUI、Jmeter、Postman三种接口测试工具的
github用相对路径显示图片_GitHub 中 readm
Windows编译g2o及其g2o viewer
解决jupyter notebook无法连接/ jupyter连接
Git恢复到之前版本
VScode常用快捷键
上一篇文章      下一篇文章      查看所有文章
加:2021-08-09 10:26:16  更:2021-08-09 10:28:22 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/16 2:57:58-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码