c++实现字符串

本文档详细介绍了如何在C++中实现自定义字符串类,包括头文件mystring.h的定义和mystring.cpp的实现,以及test.cpp用于测试的示例代码。

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

mystring.h

#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <iostream>

using namespace std;


class mystring
{
	friend ostream& operator<<(ostream& cout, mystring &str);
	friend istream& operator>>(istream& cin, mystring &str);
public:
	mystring(const mystring & str);
	mystring(const char *str);
	~mystring();

	//str=str1=str2 因为这种连环赋值所以要返回本身用引用
	mystring& operator=(const char *str);
	mystring& operator=(const mystring &str);
	
	char& operator[](int index);

	//返回值是新的数据 所以不能引用
	mystring operator+(const char *str);
	mystring operator+(const mystring &str);

	bool operator==(const char * str);
	bool operator==(const mystring &str);

private:
	char * pstring; //指向堆区的指针
	int m_size;//字符串大小 不算\0;

};

mystring.cpp

#include "mystring.h"

ostream& operator<<(ostream& cout, mystring &str)
{
	cout << str.pstring;
	return cout;
	
}

istream& operator>>(istream& cin, mystring &str)
{
	//先判断 原始是否有内容 如果有清空
	if (str.pstring != NULL)
	{
		delete[] str.pstring;
		str.pstring = NULL;
	}
	
	//让用户输入内容
	char buf[1024];
	cin >> buf;
	//把用户输入的字符赋值给 str;
	str.pstring = new char[strlen(buf) + 1];
	strcpy(str.pstring, buf);
	str.m_size = strlen(buf);

	return cin;
}



mystring::mystring(const char *str)
{
	this->pstring = new char[strlen(str) + 1];
	strcpy(this->pstring, str);
	this->m_size = strlen(str);
}
mystring::mystring(const mystring & str)
{
	this->pstring = new char[strlen(str.pstring) + 1];
	//this->pstring = new char[str.m_size + 1];
	strcpy(this->pstring, str.pstring);
	this->m_size = str.m_size;
}
mystring::~mystring()
{
	if (this->pstring != NULL)
	{
		delete[] this->pstring;
		this->pstring = NULL;
	}
}

mystring& mystring::operator=(const char *str)
{
	if (this->pstring != NULL)
	{
		delete[] this->pstring;
		this->pstring = NULL;
	}

	this->pstring = new char[strlen(str) + 1];
	strcpy(this->pstring, str);
	this->m_size = strlen(str);

	return *this;
}

mystring& mystring::operator=(const mystring &str)
{
	if (this->pstring != NULL)
	{
		delete[] this->pstring;
		this->pstring = NULL;
	}

	this->pstring = new char[strlen(str.pstring) + 1];
	strcpy(this->pstring, str.pstring);
	this->m_size = strlen(str.pstring);

	return *this;
}


char& mystring::operator[](int index)
{
	return this->pstring[index];
}


mystring mystring::operator+(const char *str)
{
	
	int newsize = this->m_size + strlen(str) + 1;

	char *tmp = new char[newsize];

	memset(tmp, 0, newsize);


	strcat(tmp, this->pstring);
	strcat(tmp, str);

	mystring  newstr(tmp);
	delete[] tmp;
	return newstr;



}

mystring mystring::operator+(const mystring &str)
{

	int newsize = this->m_size + strlen(str.pstring) + 1;

	char *tmp = new char[newsize];

	memset(tmp, 0, newsize);


	strcat(tmp, this->pstring);
	strcat(tmp, str.pstring);

	mystring  newstr(tmp);
	delete[] tmp;
	return newstr;



}



bool mystring::operator==(const char * str)
{
	if (strcmp(this->pstring, str) == 0 && this->m_size == strlen(str))
	{
		return  true;
	}
	else
	{
		return false;
	}


}
bool mystring::operator==(const mystring &str)
{
	if (strcmp(this->pstring, str.pstring) == 0 && this->m_size == strlen(str.pstring))
	{
		return  true;
	}
	else
	{
		return false;
	}
}

test.cpp

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "mystring.h"
void test()
{
	mystring str = "abc";
	cout << str << endl;

	cout << "请输入str新内容" << endl;
	cin >> str;

	cout << "新内容:" << str << endl;


	
	cout << str << endl;

	mystring str3 = "aaa";

	cout << str3 << endl;

	str3 = str = "aaaa";
	cout << str3 << endl;
	cout << str << endl;

	cout << str3[1] << endl;


	mystring str4 = "a";

	str4 =str3+str;
	
	cout << "str4=" << str4 << endl;

	if (str3 == str)
	{
		cout << "==" << endl;

	}
	else
	{
		cout << "!=" << endl;
	}



}



int main()
{
	test();
	system("pause");
	return EXIT_SUCCESS;

}

 

自己实现字符串类 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、付费专栏及课程。

余额充值