C++ 实现自己的字符串类

总时间限制: 

1000ms

 

内存限制: 

65536kB

// 在此处补充你的代码

描述

补足MyString类,使程序输出指定结果

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class MyString {
	char * p;
public:
	MyString(const char * s) {
		if( s) {
			p = new char[strlen(s) + 1];
			strcpy(p,s);
		}
		else
			p = NULL;

	}
	~MyString() { if(p) delete [] p; }
};
int main()
{
	char w1[200],w2[100];
	while( cin >> w1 >> w2) {
		MyString s1(w1),s2 = s1;
		MyString s3(NULL);
		s3.Copy(w1);
		cout << s1 << "," << s2 << "," << s3 << endl;

		s2 = w2;
		s3 = s2;
		s1 = s3;
		cout << s1 << "," << s2 << "," << s3 << endl;
		
	}
}

输入

多组数据,每组一行,是两个不带空格的字符串

输出

对每组数据,先输出一行,打印输入中的第一个字符串三次
然后再输出一行,打印输入中的第二个字符串三次

样例输入

abc def
123 456

样例输出

abc,abc,abc
def,def,def
123,123,123
456,456,456

来源

Guo Wei

 

理解深拷贝和浅拷贝,构造拷贝和运算符重载的区别

解答完整代码

#define  _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class MyString {
	char * p;
public:
	MyString(const char * s)
	{
		if (s) 
		{
			p = new char[strlen(s) + 1];
			strcpy(p, s);
		}
		else
			p = NULL;

	}
	~MyString()
	{
		if (p) delete[] p;
	}
	// 在此处补充你的代码

	MyString(const MyString &s)
	{
		if (s.p)
		{
			p = new char[strlen(s.p) + 1];
			strcpy(p, s.p);
		}
	}

	MyString & operator=(const char * s)
	{
		if (p == s)
			return *this;
		delete[] p;
		if (s)
		{
			p = new char[strlen(s) + 1];
			strcpy(p, s);
		}
		else
		{
			p = NULL;
		}
		return *this;
	}

	MyString & operator=(const MyString & s)
	{
		if (p == s.p)
			return *this;
		delete[] p;
		if (s.p)
		{
			p = new char[strlen(s.p) + 1];
			strcpy(p, s.p);
		}
		else
		{
			p = NULL;
		}
		return *this;
	}

	void Copy(const char *s)
	{
		if (p == s)
			return;
		delete[] p;
		if (s)
		{
			p = new char[strlen(s) + 1];
			strcpy(p, s);
		}
		else
		{
			p = NULL;
		}
	}

	friend ostream & operator<<(ostream &os, MyString & s)
	{
		os << s.p;
		return os;
	}

};
int main()
{
	char w1[200], w2[100];
	while (cin >> w1 >> w2) {
		MyString s1(w1), s2 = s1;    // 拷贝构造
		MyString s3(NULL);
		s3.Copy(w1);                 // 成员函数深拷贝
		cout << s1 << "," << s2 << "," << s3 << endl;

		s2 = w2;     // 运算符重载
		s3 = s2;     
		s1 = s3;
		cout << s1 << "," << s2 << "," << s3 << endl;

	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值