前面说过,C++提供了string类,里面实现了对字符串的各种操作,string类的实现同时也是再面试和笔试的时候比较场考的一类题型。这次的博客就来进行C++里面string类的实现,因为主要是重载里面的一些运算符,所以也算是对运算符重载的练习。
直接来看代码:
String.h
#ifndef STRING_H
#define STRING_H
class Mstring
{
public:
Mstring(const char* );//构造函数重载
Mstring(const Mstring& src);//拷贝构造函数的重载
Mstring& operator=(const Mstring &src);//等号运算符重载
void show();//输出字符串
char& operator[](const int sit);//下标运算符的重载
char& operator*();//解引用运算符重载
Mstring operator+(const Mstring &src);加号运算符重载(str1 + str2)
Mstring operator+(char *str);(str1 + "hello")
ostream& operator<<(/*this*/ostream& out);重载输出运算符
istream& operator>>(istream& in);//重载输入运算符
~Mstring();//析构函数
bool operator==(const Mstring&s);//等号运算符重载
bool operator>(const Mstring&s);//大于运算符重载
bool operator>=(const Mstring &s);//大于等于运算符重载
bool operator<(const Mstring &s);//小于运算符重载
bool operator<=(const Mstring &s);//小于等于运算符重载
private:
char* _str;
int _len;
friend Mstring operator+(char* str, Mstring& src);
friend ostream& operator<<(ostream& out, const Mstring& str);
};
#endif
String.cpp
#include"String.h"
#include<iostream>
#include<stdio.h>
#include<string.h>
Mstring::Mstring(const char* str = NULL)
{
if (NULL == str)
{
_len = 0;
_str = NULL;
}
else
{
_len = strlen(str);
_str = new char[_len + 1];
strcpy_s(_str, _len + 1, str);
}
}
Mstring::Mstring(const Mstring& src)
{
if (NULL == src._str)
{
_len = 0;
_str = NULL;
}
else
{
_len = src._len;
_str = new char[_len + 1];
strcpy_s(_str, _len + 1, src._str);
}
}
Mstring::Mstring& operator=(const Mstring &src)
{
if (this == &src)
{
return *this;
}
if (NULL != _str)
{
delete[]_str;
}
if (NULL == src._str)
{
_len = 0;
_str = NULL;
}
else
{
_len = src._len;
_str = new char[_len + 1];
strcpy_s(_str, _len + 1, src._str);
}
return *this;
}
Mstring::void show()
{
for (int i = 0; i < _len; i++)
{
cout << _str[i];
}
cout << endl;
}
Mstring::char& operator[](const int sit)
{
if (sit >= _len)
{
throw "memory over flow";
}
return _str[sit];
}
Mstring::char& operator*()
{
if (NULL == _str)
{
throw "str is empty";
}
return *_str;
}
Mstring::Mstring operator+(const Mstring &src)
{
char* buff = new char[_len + src._len + 1];
strcpy_s(buff, _len + 1, _str);
strcpy_s(buff + _len, src._len + 1, src._str);
Mstring tmp(buff);
delete []buff;
return tmp;
}
Mstring::Mstring operator+(char *str)/* this , str */
{
char* buff = new char[_len + strlen(str) + 1];
strcpy_s(buff, _len + 1, _str);
strcpy_s(buff + _len, strlen(str) + 1, str);
Mstring tmp(buff);
delete[]buff;
return tmp;
}
Mstring::ostream& operator<<(/*this*/ostream& out)//cout << str3 << endl;
{
out << _str;
return out;
}
Mstring::istream& operator>>(istream& in)
{
int i = 0;
int len = 10;
char *buff = new char[len];
char c = '0';
while (1)
{
if (i == len-1)
{
char *tmp = new char[len * 2];
memcpy_s(tmp, len, buff,len);
delete[]buff;
len *= 2;
buff = tmp;
}
c = getchar();
if ('\n' == c)
{
buff[i] = 0;
break;
}
buff[i] = c;
i++;
}
_str[i] = 0;
*this = buff;
delete[]buff;
return in;
}
Mstring::~Mstring()
{
if (NULL != _str)
{
delete[]_str;
}
}
Mstring::bool operator==(const Mstring&s)
{
const char * p1 = _str;
const char * p2 = s._str;
while(*p1!=NULL && *p2 != NULL)
{
if(*p1 != *p2)
{
return false;
}
else
{
p1 ++;
p2 ++;
}
}
if(*p1 == '\0' && *p2 == '\0')
{
return true;
}
else
{
return false;
}
}
Mstring::bool operator>(const Mstring&s)
{
char * p1 = _str;
char * p2 = s._str;
while(*p1 !=NULL && *p2 != NULL)
{
if(*p1 > *p2)
{
return true;
}
else if(*p1 < *p2)
{
return false;
}
else
{
p1++;
p2++;
}
}
if(p1 != '\0' && p2 != '\0')
{
return true;
}
else
{
return false;
}
}
Mstring::bool operator>=(const Mstring &s)
{
return (*this > s || *this == s);
}
Mstring::bool operator<(const Mstring &s)
{
return !(*this >= s);
}
Mstring::bool operator<=(const Mstring &s)
{
return !(*this > s);
}
Mstring operator+(char* str, Mstring& src)
{
char* buff = new char[strlen(str) + src._len + 1];
strcpy_s(buff, strlen(str) + 1, str);
strcpy_s(buff + strlen(str), src._len + 1, src._str);
Mstring tmp = buff;
delete[]buff;
return tmp;
}
ostream& operator<<(ostream& out,const Mstring& str)//cout << str3 << endl;
{
out << str._str;
return out;
}
istream& operator>>(istream& in, Mstring &str)
{
int i = 0;
int len = 10;
char *buff = new char[len];
char c = '0';
while (1)
{
if (i == len - 1)
{
char *tmp = new char[len * 2];
memcpy_s(tmp, len, buff, len);
delete[]buff;
len *= 2;
buff = tmp;
}
c = getchar();
if ('\n' == c)
{
buff[i] = 0;
break;
}
buff[i] = c;
i++;
}
str = buff;
delete[]buff;
return in;
}
main.cpp
#include<iostream>
#include"String.h"
#include"String.cpp"
int main()
{
Mstring str1("asd");
cout << "str1 = " ;
str1.show();cout << "\n" ;
Mstring str2("agd");
cout << "str2 = " ;
str2.show();cout << "\n" ;
//Mstring str3;
//str3 = str1;
//str3.show();
/*if(str1 == str2)
{
cout << "str1 == str2" << endl;
}
else
{
cout << "str1 != str2" << endl;
}
*/
if(str1 > str2)
{
cout << "str1 > str2" << endl;
}
else
{
cout << "str1 < str2" << endl;
}
//str1[3];
/*
str1[2] = 'a';
str1.show();
*str1 = 'k';
str1.show();
str3 = str1 + str2;
str3.show();
str3 = str1 + "aaaaa";
str3 = str3.operator+("bbbb");
str3.show();
str3 = "qqqqq" + str3;
str3.show();
cout << "sss" << endl;
cout << str3 << endl;
str3.operator<<(cout) << endl;
//str3.operator>>(cin);
//cout << str3 << endl;
//str3.show();
cin >> str3;
cout << str3 << endl;
//A+B
//ostream out(cout);
//out << "aaa" << endl;
char arr[10] = {0};
cin >> arr;
int num;
while (1)
{
cin >> num;
//cout << "aaaaaaaaaaaa" << endl;
switch (num)
{
case 1:
cout << 1 << endl;
break;
case 2:
cout << 2 << endl;
break;
default:
break;
}
}
*/
return 0;
}