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");
}
运行结果: