在用c++语言时,相信平时使用最多的库类,string一定是其中之一了。那么磨其锋方能知其利,今天我们在这里自制一个我们自己的string类,相比正版的string,这个盗版的string会简陋很多,但是通过实现其中一些普遍的功能,我们会在之后使用正版string时更有心得。
所谓的string,可以理解为字符串的意思,字符串嘛,即一个字符数组,一个合格的字符数组,里面应该有一个结束符,如【\0】,还应该能当做字符数组使用,比如使用【[]】符号来获取字符串的某个字符,甚至从某种角度上来说,可以被当成数值看待,比如可以拿两个字符串来比较它们是否相等或谁大于谁(按字母表排序)。
了解了上面的话后,就知道我们应该给这个string类实现哪些功能了。所以首先我们来创建一个头文件,里面是这个string类的声明:
#include <iostream>
using namespace std;
class MyString
{
private:
int len;//这个是string的长度
char *str;
static int str_num;//这个表示我们创建了多少的字符串;
static const int MAXC = 100;//每个string最多能有多少字符
public:
MyString();
MyString(const char *);
MyString(const MyString &);
~MyString();
MyString operator=(const char *);
MyString operator=(const MyString &);
char operator[](int i);
int getlen(){return len;}
int getnum(){return str_num};
friend bool operator<(const MyString &, const MyString &);
friend bool operator>(const MyString &, const MyString &);
friend bool operator==(const MyString &, const MyString &);
friend istream & operator>>(istream &, MyString &);
friend ostream & operator<<(ostream &,MyString &);
}
下面就是定义我们的MyString类了,首先我们要给里面的静态变量str_num在全局域初始化赋值
int MyString::str_num=0;
然后考虑用字符串或本类当参数的构造函数,因为里面的【str】使用的字符指针的缘故,所以我们的构造函数一定要是深拷贝,如果我们用浅拷贝的话(仅拷贝字符串指针),那么很可能会造成内存泄漏甚至程序崩溃:
MyString::MyString(const char *s)
{
int l = strlen(s);
str = new char[l + 1];
for (int i = 0; i < l; i++)
str[i] = s[i];
str[l] = '\0';
str_num++;
}
MyString::MyString(const MyString &s)
{
int l = strlen(s.str);
str = new char[l + 1];
for (int i = 0; i < l; i++)
str[i] = s.str[i];
str[l] = '\0';
str_num++;
}
MyString::~MyString()
{
delete[] str;
str_num--;
}
同理,接下来是两个复制操作符重载函数:
MyString & MyString::operator=(const char * s)
{
delete[] str;
int l = strlen(s);
str = new char[l + 1];
for (int i = 0; i < l; i++)
str[i] = s[i];
str[l] = '\0';
str_num++;
return *this;
}
MyString & MyString::operator=(const MyString & s)
{
if (this == &s) return *this;
delete[] str;
int l = strlen(s.str);
str = new char[l + 1];
for (int i = 0; i < l; i++)
str[i] = s.str[i];
str[l] = '\0';
str_num++;
return *this;
}
接着是获取单个字符的函数实现
char MyString::operator[](int i)
{
if(i > MAXC)
{
cout<<"超出了字符串最长,请检查“<<endl;
return '\0';
}
return str[i];
}
现在到了比较大小了,我们可以借助库函数strcmp实现:
bool operator<(const MyString & s1, const MyString & s2)
{
return((strcmp(s1.str, s2.str) < 0));
}
bool operator>(const MyString & s1, const MyString & s2)
{
return(strcmp(s1.str, s2.str) > 0);
}
bool operator==(const MyString & s1, const MyString & s2)
{
return (strcmp(s1.str, s2.str) == 0);
}
最后,我们需要像实现string一样实现我们类的输入输出功能:
ostream & operator<<(ostream & os, MyString & s)
{
return os << s.str;
}
istream & operator>>(istream & is, MyString & s)
{
char temp[MyString::MAXC];
is.get(temp, MyString::MAXC);
if (is)
s = temp;
while (is && is.get() != '\n')
continue;
return is;
}
以上全做好之后,我们只需写个小程序测试一下我们的类就可以了,小程序的代码就不单拉出来了,只需直接把整合好的源代码放上去就行了:
//头文件MyString.h
#include <iostream>
using namespace std;
class MyString
{
private:
int len;//这个是string的长度
char *str;
static int str_num;
static const int MAXC = 100;//每个string最多能有多少字符
public:
MyString();
MyString(const char *);
MyString(const MyString &);
~MyString();
MyString & operator=(const char *);
MyString & operator=(const MyString &);
char operator[](int i);
int getlen() { return len; };
int getnum() { return str_num; };
friend bool operator<(const MyString &, const MyString &);
friend bool operator>(const MyString &, const MyString &);
friend bool operator==(const MyString &, const MyString &);
friend istream & operator>>(istream &, MyString &);
friend ostream & operator<<(ostream &,MyString &);
};
//类的实现文件MyString.cpp
#include "MyString.h"
int MyString::str_num = 0;
MyString::MyString(const char *s)
{
int l = strlen(s);
str = new char[l + 1];
for (int i = 0; i < l; i++)
str[i] = s[i];
str[l] = '\0';
str_num++;
}
MyString::MyString(const MyString &s)
{
int l = strlen(s.str);
str = new char[l + 1];
for (int i = 0; i < l; i++)
str[i] = s.str[i];
str[l] = '\0';
str_num++;
}
MyString::~MyString()
{
delete[] str;
str_num--;
}
MyString & MyString::operator=(const char * s)
{
delete[] str;
int l = strlen(s);
str = new char[l + 1];
for (int i = 0; i < l; i++)
str[i] = s[i];
str[l] = '\0';
str_num++;
return *this;
}
MyString & MyString::operator=(const MyString & s)
{
if (this == &s) return *this;
delete[] str;
int l = strlen(s.str);
str = new char[l + 1];
for (int i = 0; i < l; i++)
str[i] = s.str[i];
str[l] = '\0';
str_num++;
return *this;
}
char MyString::operator[](int i)
{
if (i > MAXC)
{
cout << "超出了字符串最长,请检查"<<endl;
return '\0';
}
return str[i];
}
bool operator<(const MyString & s1, const MyString & s2)
{
return((strcmp(s1.str, s2.str) < 0));
}
bool operator>(const MyString & s1, const MyString & s2)
{
return(strcmp(s1.str, s2.str) > 0);
}
bool operator==(const MyString & s1, const MyString & s2)
{
return (strcmp(s1.str, s2.str) == 0);
}
ostream & operator<<(ostream & os, MyString & s)
{
return os << s.str;
}
istream & operator>>(istream & is, MyString & s)
{
char temp[MyString::MAXC];
is.get(temp, MyString::MAXC);
if (is)
s = temp;
while (is && is.get() != '\n')
continue;
return is;
}
//我们最后的源文件
#include "MyString.h"
int main()
{
MyString s1("helloooworld");
MyString s2("C++ String");
if (s1 > s2)
{
cout << "还是s1字符串较大:" << s1 << endl;
}
cout << "其中位于中间的字符是:" << endl;
cout << s1[(s1.getlen() / 2)] << endl;
cout << "现在重新输入s1(只要英语):" << endl;
cin >> s1;
cout << s1;
return 0;
}