用C++手动简单实现string类

本文介绍了如何使用C++实现一个名为myString的类,包含构造、拷贝构造、c_str/data接口、字符串连接、比较功能以及手动析构释放内存。实例展示了如何创建字符串、操作字符串并演示了重要函数的使用。

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

#include<iostream>
#include<cstring>
using namespace std;

class myString {
public:
	myString(const char* str = "") {
		strSize = strlen(str) + 1;
		this->str = new char[strSize];
		strcpy_s(this->str, strSize, str);
	}
	myString(const myString& object) {
		strSize = object.strSize;
		this->str = new char[strSize];
		strcpy_s(this->str, strSize, object.str);
	}
	char* c_str() {
		return str;
	}
	char* data() {
		return str;
	}

	~myString(){
		delete[] str;
		str = nullptr;
	}
	myString append(const myString& object) {
		myString temp;
		temp.strSize = myString::strSize + object.strSize - 1;
		temp.str = new char[temp.strSize];
		memset(temp.str, 0, temp.strSize);
		strcat_s (temp.str, temp.strSize, myString::str);
		strcat_s (temp.str, temp.strSize, object.str);
		return temp;
		

	}
	int compare(const myString& object) {
		return strcmp(myString::str, object.str);
	}
protected:
	char* str;
	int strSize;
};
//1.实现string中创建方式
int main() {
	myString str1;
	myString str2("ILoveyou");
	myString str3(str1);
	myString str4 = str2;
	//2.通过实现data和c_str函数 打印字符串

	cout << str2.c_str() << endl;  //打印ILoveyou
	cout << str2.data() << endl;   //打印ILoveyou
	//3.实现append 实现字符串的链接
	myString strOne = "one";
	myString strTwo = "two";
	myString strThree = strOne.append(strTwo);
	cout << strThree.data() << endl;	    //onetwo
	//4.实现字符串比较
	cout << strOne.compare(strOne) << endl;	//0
	//5.手写析构函数释放内存
	return 0;
}

自己实现的字符串 class CMStringImp; class CMstring { public: explicit CMstring(void); ~CMstring(void); CMstring(LPCTSTR lpszstr); CMstring(const CMstring& lpszstr); CMstring& operator = (const CMstring& lpszstr); operator LPCTSTR() const; bool operator == (const CMstring&) const; bool operator != (const CMstring&) const; bool operator < (const CMstring&) const; TCHAR operator[] (int nIndex) const; TCHAR& operator[] (int nIndex); CMstring& operator += (LPCTSTR pStr); CMstring& operator += (TCHAR ch); friend CMstring operator+(const CMstring& str1, const CMstring& str2); friend CMstring operator+(const CMstring& str1, LPCTSTR lpszstr); friend CMstring operator+(const CMstring& str1, TCHAR ch); friend CMstring operator+(TCHAR ch, const CMstring& str1); friend CMstring operator+(LPCTSTR lpszstr, const CMstring& str1); // friend wostream operator <<(wostream &is;,const CMstring &str;); public: LPCTSTR GetData() const; bool IsEmpty() const; TCHAR GetAt(int) const; TCHAR& GetAt(int); int GetLength() const; int Compare(const CMstring&) const; int CompareNoCase(const CMstring&) const; int Find(TCHAR ch, int nStart = 0) const; int Find(LPCTSTR pStr, int nStart = 0) const; int ReverseFind(TCHAR ch) const; int ReverseFind(LPCTSTR pStr) const; CMstring Right(int nCount) const; CMstring Left(int nCount ) const; public: CMstring& MakeLower(); CMstring& MakeUpper(); CMstring& MakeReverse(); int Replace(TCHAR chOld, TCHAR chNew); int Replace(LPCTSTR pszOld, LPCTSTR pszNew); int Insert(int iIndex, TCHAR ch); void Format(LPCTSTR lpszFormat, ...); private: CMStringImp* m_pImp; };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值