019:全面的MyString

这道题是运算符重载、动态内存分配的很好的例题

#include <cstdlib>
#include <iostream>
using namespace std;
int strlen(const char * s) 
{	int i = 0;
	for(; s[i]; ++i);
	return i;
}
void strcpy(char * d,const char * s)
{
	int i = 0;
	for( i = 0; s[i]; ++i)
		d[i] = s[i];
	d[i] = 0;
		
}
int strcmp(const char * s1,const char * s2)
{
	for(int i = 0; s1[i] && s2[i] ; ++i) {
		if( s1[i] < s2[i] )
			return -1;
		else if( s1[i] > s2[i])
			return 1;
	}
	return 0;
}
void strcat(char * d,const char * s)
{
	int len = strlen(d);
	strcpy(d+len,s);
}
class MyString
{
// 在此处补充你的代码
};


int CompareString( const void * e1, const void * e2)
{
	MyString * s1 = (MyString * ) e1;
	MyString * s2 = (MyString * ) e2;
	if( * s1 < *s2 )
	return -1;
	else if( *s1 == *s2)
	return 0;
	else if( *s1 > *s2 )
	return 1;
}
int main()
{
	MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
	MyString SArray[4] = {"big","me","about","take"};
	cout << "1. " << s1 << s2 << s3<< s4<< endl;
	s4 = s3;
	s3 = s1 + s3;
	cout << "2. " << s1 << endl;
	cout << "3. " << s2 << endl;
	cout << "4. " << s3 << endl;
	cout << "5. " << s4 << endl;
	cout << "6. " << s1[2] << endl;
	s2 = s1;
	s1 = "ijkl-";
	s1[2] = 'A' ;
	cout << "7. " << s2 << endl;
	cout << "8. " << s1 << endl;
	s1 += "mnop";
	cout << "9. " << s1 << endl;
	s4 = "qrst-" + s2;
	cout << "10. " << s4 << endl;
	s1 = s2 + s4 + " uvw " + "xyz";
	cout << "11. " << s1 << endl;
	qsort(SArray,4,sizeof(MyString),CompareString);
	for( int i = 0;i < 4;i ++ )
	cout << SArray[i] << endl;
	//s1的从下标0开始长度为4的子串
	cout << s1(0,4) << endl;
	//s1的从下标5开始长度为10的子串
	cout << s1(5,10) << endl;
	return 0;
}

样例输入

样例输出

  1. abcd-efgh-abcd-
  2. abcd-
  3. abcd-efgh-
  4. efgh-
  5. c
  6. abcd-
  7. ijAl-
  8. ijAl-mnop
  9. qrst-abcd-
  10. abcd-qrst-abcd- uvw xyz
    about
    big
    me
    take
    abcd
    qrst-abcd-

分析

  1. 需要在private声明char* ptr
  2. 构造函数MyString(const char* s)MyString()以及MyString(const MyString& s),其中,默认构造函数不是空字符nullptr,而是有一个""的字符…,然后要有析构函数
  3. 需要用到的运算符:[ ]类.operator+(类)
    = (类或字符串指针)<<+=( )
  4. 需要注意是返回引用还是返回值,这个特别要注意。如果返回引用则会修改*this的值,如果返回值则只是暂时的副本。
  5. 返回bool的== > <运算符。
  6. 最好要讨论是否为空指针的情况,然后再决定是否分配内存。
if(p)
{	ptr = new char[strlen(p)];
	strcpy(ptr,p);}
else
{	ptr = new char[1];
	ptr[0]='\0';}
private:
	char *p;
public:
	MyString(const char * s = NULL)
	{
    
    
		if (s)
		{
    
    
			p = new char[strlen(s) + 1];
			strcpy(p, s);
		}
		else
		{
    
    
			p = new char[1];
			p[0] = '\0';
		}
	}
	~MyString()
	{
    
    
		if (p) delete[] p;
	}
	MyString(const MyString &s)
	{
    
    
		if (s.p)
		{
    
    
			p = new char[strlen(s.p) + 1];
			strcpy(p, s.p);
		}
		else
		{
    
    
			p = new char[1];
			p[0] = '\0';
		}
	}
	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 = new char[1];
			p[0] = '\0';
		}
		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 = new char[1];
			p[0] = '\0';
		}
		return *this;
	}
	friend MyString  operator+(MyString &a, MyString &b)
	{
    
    
		char *temp = new char[strlen(a.p) + strlen(b.p)];
		strcpy(temp, a.p);
		strcat(temp, b.p);
		return MyString(temp);
	}
	friend ostream & operator<<(ostream &os, MyString & s)
	{
    
    
		os << s.p;
		return os;
	}
	char & operator[](int i)
	{
    
    
		return p[i];
	}
 
	MyString & operator+=(const char *s)
	{
    
    
		char* temp = new char[strlen(p)+1];
		strcpy(temp, p);
		delete[] p;
		p = new char[strlen(temp) + strlen(s)+1];
		strcpy(p, temp);
		strcat(p, s);
		return *this;
	}
 
	friend MyString  operator+(const char * str, MyString s )
	{
    
    
		char *temp = new char[strlen(str) + strlen(s.p)+1];
		strcpy(temp,str);
		strcat(temp, s.p);
 
		return MyString(temp);
 
	}
 
	friend MyString operator+(MyString s, const char *str)
	{
    
    
		char *temp = new char[strlen(s.p) + strlen(str) + 1];
		strcpy(temp, s.p);
		strcat(temp, str);
		return MyString(temp);
	}
 
	friend bool operator<(MyString &s1, MyString &s2)
	{
    
    
		if (strcmp(s1.p, s2.p) < 0)
			return true;
		else
			return false;
	}
 
	friend bool operator>(MyString &s1, MyString &s2)
	{
    
    
		if (strcmp(s1.p, s2.p) > 0)
			return true;
		else
			return false;
	}
 
	friend bool operator==(MyString &s1, MyString &s2)
	{
    
    
		if (strcmp(s1.p, s2.p) == 0)
			return true;
		else
			return false;
	}
 
	char * operator () (int start, int length)
	{
    
    
		char * temp = new char[length+1];
		for (int i = start; i < start+length; i++)
		{
    
    
			temp[i - start] = p[i];
		}
		temp[length] = '\0';
		return temp;}

最后一个()运算符重载值得学习.

猜你喜欢

转载自blog.csdn.net/ZmJ6666/article/details/108601323