1.MyString.h
#pragma once
#include<iostream>
using namespace std;
class MyString
{
public:
MyString(const char *str = NULL);
MyString(const MyString&another);
MyString&operator=(const MyString&another);
char* c_str();
MyString operator+(const MyString&another);
bool operator>(const MyString&another);
bool operator<(const MyString&another);
bool operator==(const MyString&another);
char &operator[](int idx);
char at(int idx);
friend istream& operator>>(istream& in,MyString & str);
friend ostream& operator<<(ostream& out,const MyString & str);
~MyString(void);
private:
char *_str;
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
2.MyString.cpp
#include "MyString.h"
MyString::MyString(const char *str){
if(str == NULL)
{
_str = new char[1];
_str[0] = '\0';
}
else
{
int len = strlen(str);
_str = new char[len+1];
strcpy(_str,str);
}
}
MyString::MyString (const MyString&another)
{
int len = strlen(another._str);
this->_str = new char[len+1];
strcpy(this->_str,another._str);
}
MyString&MyString::operator=(const MyString&another)
{
if(this == &another)
{
return *this;
}
else{
delete []this->_str;
int len = strlen(another._str);
this->_str = new char[len+1];
strcpy(this->_str,another._str);
return *this;
}
}
char* MyString::c_str()
{
return this->_str;
}
MyString MyString::operator + (const MyString& another)
{
MyString mystr;
int len = strlen(this->_str)+strlen(another._str);
delete mystr._str;
mystr._str = new char[len+1];
memset(mystr._str,0,len+1);
strcat(mystr._str,this->_str);
strcat(mystr._str,another._str);
return mystr;
}
bool MyString::operator>(const MyString& another)
{
if(strcmp(this->_str,another._str)>0)
return true;
else return false;
}
bool MyString::operator<(const MyString& another)
{
if(strcmp(this->_str,another._str)<0)
return true;
else return false;
}
bool MyString::operator==(const MyString& another)
{
if(strcmp(this->_str,another._str)==0)
return true;
else return false;
}
char & MyString::operator[](int idx)
{
return this->_str[idx];
}
char MyString::at(int idx)
{
return this->_str[idx];
}
istream& operator>>(istream& in,MyString & str)
{
delete []str._str;
char buf[1024];
scanf("%s",buf);
int len = strlen(buf);
str._str = new char[len+1];
strcpy(str._str,buf);
return in;
}
ostream& operator<<(ostream& out,const MyString & str)
{
out<<str._str;
return out;
}
MyString::~MyString(void)
{
delete []this->_str;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
3.main.cpp
#include<iostream>
#include "MyString.h"
using namespace std;
int main()
{
MyString s1;
cin>>s1;
cout<<s1<<endl;
cout<<s1.c_str()<<endl;
MyString s2("hello");
cout<<"s2="<<s2.c_str()<<endl;
MyString s3(s2);
cout<<s3.c_str()<<endl;
MyString s4 = " world";
cout<<"s4="<<s4.c_str()<<endl;
MyString s5 = s3 + s4;
cout<<s5.c_str()<<endl;
if(s2<s4)
cout<<"s2<s4"<<endl;
else if(s2>s4)
cout<<"s2>s4"<<endl;
else
cout<<"s2==s4"<<endl;
system("pause");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
其实MyString类是模范C++的string类写的,string类里面有很多的成员函数,而我只实现了其中的一小部分常见的功能函数,如果有兴趣可以自己试着扩展更多的功能。