自己寫一個簡易的string類型

C語言中沒有string類型,於是,在使用C語言的時候,總會用char*類型來替代string類型,但是char*和string類型還是有一定的不同的。最大的差異就是在對字符串的操作上。顯然:比起char* ,string對於字符串的操作更加的簡便,比如說:

1.兩個string類的變量str1和str2,我只需要寫str1+str2即可完成兩個字符串的連接,但是char*就不可以。要用char*類型的話,恐怕還得用strcat函數。

2.兩個string類的變量str1和str2,我寫str1 = str2就可直接把str2值賦給str1,在這點上貌似char*也可以(私下裏試了一下)。

3.兩個string類的變量str1和str2,我用str1>str2,str1<str2,str1==str2等操作,就可以直接實現兩個字符串之間的比較。char*的話,恐怕還得用strcmp函數。

……

本人能力有限,暫時無法一下子就實現string的全部功能,這裏只寫了一部分,請大家多多包涵。爲了與string區分開,我將這個自創的string類型命名爲myString。


首先得定義一個myString類作爲頭文件,裏面包含以上這些操作的重載,代碼如下:

//myString.h

#pragma once
#include<iostream>
using std::ostream;
using std::istream;

class myString
{
public:
	//構造函數
	myString();
	myString(const char* str);
	myString(const myString& other); //拷貝構造函數

	//析構函數
	~myString();

	//賦值操作的重載
	myString & operator = (const myString& other);
	myString & operator = (const char* str);

	//連接操作的重載
	myString  operator + (const char* other);
	myString  operator + (const myString& other);

	//比較操作的重載
	bool operator<(const myString& other);
	bool operator>(const myString& other);
	bool operator==(const myString& other);

	//獲取長度,相當於string類型當中的length();
	int getLength();
	
	//輸入輸出的重載
	friend ostream & operator << (ostream& os, const myString& str);
	friend istream & operator >> (istream& is, myString& str);
private:
	char* myStr;
};

/*
	ostream,istream爲什麼要用friend?
	注意:要實現輸入輸出功能的話,你只能用cout或者cin來實現,而cout和cin並不是myString類裏的東西
所以你需要用friend使得myString有權訪問cout和cin。
	爲什麼operator裏面是兩個參數?cout<<以及cin>>中的“cin”和"cout"是三目運算符,所以必須是兩個參數才能實現其功能。
*/

然後,你需要在myString.cpp文件中一一實現折現重載函數,不過在寫的過程中我還是遇到了不少問題,這些問題我已經寫在的代碼當中,等過一陣子我再做解答。代碼如下:


//myString.cpp

#pragma warning(disable:4996)
#include<cstring>
#include<cstdio>
#include"myString.h"


myString::myString( )
{
	myStr = new char[1];
	*myStr = '\0';
}
myString::myString(const char* str = NULL)
{
	if (str ==NULL)
	{
		myStr = new char[1];
		*myStr = '\0';
	}
	else
	{
		int length = strlen(str);
		myStr = new char[length + 1];
		strcpy(myStr, str);
	}
}

myString::myString(const myString & other)
{
	int length = strlen(other.myStr);
	myStr = new char[length + 1];
	strcpy(myStr, other.myStr);
}

myString::~myString()
{
	delete[] myStr;
}

myString & myString::operator=(const myString & other)
{
	if (this == &other)
	{
		return *this;
	}
	else
	{
		delete[] myStr;
		int length = strlen(other.myStr);
		myStr = new char[length + 1];
		strcpy(myStr, other.myStr);
		return *this;
	}
}
myString & myString::operator = (const char* str)
{
	delete[] myStr;
	int length = strlen(str);
	myStr = new char[length + 1];
	strcpy(myStr, str);
	return *this;
}

myString  myString::operator+(const myString & other)
{
	char* temp = NULL;
	int length = strlen(myStr);
	int other_length = strlen(other.myStr);
	temp = new char[length + other_length + 1];
	strcpy(temp, myStr);
	strcat(temp, other.myStr);
	temp[length + other_length] = '\0';
	return myString(temp);
}
myString myString::operator + (const char* other)  //如果這裏返回一個&類型的話,程序運行就會出現異常,爲什麼?
{
	char* temp = NULL;
	int length = strlen(myStr);
	int other_length = strlen(other);
	temp = new char[length + other_length + 1];
	strcpy(temp, myStr);
	strcat(temp, other);
	temp[length + other_length] = '\0';
	return myString(temp);
}


bool myString::operator<(const myString & other)
{
	if (strcmp(myStr, other.myStr) < 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool myString::operator>(const myString & other)
{
	if (strcmp(myStr, other.myStr)>0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool myString::operator==(const myString & other)
{
	if (strcmp(myStr, other.myStr))
	{
		return true;
	}
	else
	{
		return false;
	}
}

int myString::getLength()
{
	return strlen(myStr);
}

ostream & operator<<(ostream & os, const myString & str)
{
	os << str.myStr;
	return os;
}

istream & operator>> (istream & is, myString & str)
{
	char* temp = new char[1024];//我現在也在思考一個問題,若超出1024了該怎麼辦?
	is.get(temp, 1024);
	if (is)
	{
		str.myStr = temp;
	}
	while (is&&is.get() != '\n')
	{
		continue;
	}
	return is;
}

之後,寫一個主函數檢驗一下。

<pre name="code" class="cpp">#include"myString.h"
#include<string>
#include<cstring>
#include<iostream>
using namespace std;

void main()
{
	{

		myString str1;
		str1 = "good";
		cout << str1 << "\n";

		myString str2;
		str2 = "weather";
		str1 = str2;
		cout << str1 << "\n";

		str1 = "good";
		myString str3;
		str3 = str1 + str2;
		cout << str3 << "\n";

		myString str4;
		cout << "輸入一個字符串:";
		cin >> str4;
		cout <<"您輸入的字符串是:"<< str4 << "\n";
	} //打上這個大括號是爲了檢驗析構函數是否運行正常。

	system("pause");
}



運行結果:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章