c++ 模仿string类
#include <iostream>
#include <cstring>
using namespace std;
//自定义 myString
class myString{
private:
char *str;//字符串首地址
int _size;//字符串长度
public:
//无参构造
myString():_size(0){
str = new char[_size+1];
*str = '\0';
}
// //有参构造
myString(int size):_size(size){
str = new char[_size+1];
*str = '\0';
}
//有参构造
myString(const char *s){
//cout<<"myString(const char *s)"<<endl;
if(s == nullptr){
_size=0;
str = new char[_size+1];
*str = '\0';
}else{
_size = strlen(s);
str = new char[_size+1];
strcpy(str,s);
}
}
//析构函数
~myString(){
delete [] str;
}
//拷贝构造函数
myString(const myString &other){
_size = other._size;
str = new char[_size+1];
strcpy(str,other.str);
}
//拷贝赋值函数
myString &operator=(const myString &other){
if(this != &other){
_size = other._size;
//先释放
delete [] str;
//再申请
str = new char[_size+1];
strcpy(str,other.str);
}
return *this;
}
//重载<<
friend ostream& operator<<(ostream& _ostream,const myString &ms){
return _ostream<< ms.str;
}
//重载>>
friend istream& operator>>(istream& _istream,const myString &ms){
return _istream.getline(ms.str,ms.size(),'\n');
}
friend istream& getline(istream& _istream,const myString &ms){
return _istream.getline(ms.str,ms.size(),'\n');
}
//重载 ==
bool operator==(const myString &other) const{
return str == other.str;
}
//重载 !=
bool operator!=(const myString &other) const{
return str != other.str;
}
//重载 <
bool operator<(const myString &other) const{
return strcmp(str,other.str)<0;
}
//重载 <=
bool operator<=(const myString &other) const{
return strcmp(str,other.str)<=0;
}
//重载 >
bool operator>(const myString &other) const{
return strcmp(str,other.str)>0;
}
//重载 >=
bool operator>=(const myString &other) const{
return strcmp(str,other.str)>=0;
}
//重载 +
const myString operator+(const myString &other) const{
int new_size = this->_size + other._size;
myString new_string(new_size);
strcat(new_string.str,this->str);
strcat(new_string.str,other.str);
return new_string;
}
const myString operator+(const char *other) const{
int new_size = this->_size + strlen(other);
myString new_string(new_size);
strcat(new_string.str,this->str);
strcat(new_string.str,other);
return new_string;
}
//重载 +=
const myString& operator+=(const myString &other){
int new_size = this->_size + other._size;
myString new_string(new_size);
strcat(new_string.str,this->str);
strcat(new_string.str,other.str);
*this = new_string;
return *this;
}
const myString& operator+=(const char *other){
int new_size = this->_size + strlen(other);
myString new_string(new_size);
strcat(new_string.str,this->str);
strcat(new_string.str,other);
*this = new_string;
return *this;
}
//重载 []
const char &operator[](const int pos) const{
return str[pos];
}
//判空
bool empty() const{
if(_size == 0){
return true;
}else{
return false;
}
}
//size函数
int size() const{
return _size;
}
//c_str函数
const char *c_str() const{
return str;
}
//at函数
const char &at(int pos) const{
return str[pos];
}
};
int main()
{
cout<<"无参构造:"<<endl;
myString s0;
cout<<"s0="<<s0<<endl;
cout<<"有参构造:"<<endl;
myString s1("hello");
cout<<"s1="<<s1<<endl;
cout<<"拷贝构造:"<<endl;
myString s2(s1);
cout<<"s2="<<s2<<endl;
cout<<"判空+拷贝赋值+size+c_str+at:"<<endl;
myString s3;
if(s3.empty()){
s3 = s2;
}
cout<<"s3="<<s3.c_str()<<endl;
cout<<"s3.size()="<<s3.size()<<endl;
cout<<"s3.at(1)="<<s3.at(1)<<endl;
//cout<<"s3.at(10)="<<s3.at(10)<<endl;
cout<<"运算符重载:"<<endl;
if(s3 == s3){
cout<<"s3 == s3"<<endl;
}
if(s3 != s2){
cout<<"s3 != s2"<<endl;
}
myString s4("hello aaa");
myString s5("hello bbb");
myString s6;
cout<<"s4="<<s4<<endl;
cout<<"s5="<<s5<<endl;
if(s5 > s4){
cout<<"s5 > s4"<<endl;
}
if(s4 >= s4){
cout<<"s4 >= s4"<<endl;
}
if(s4 < s5){
cout<<"s4 < s5"<<endl;
}
if(s5 <= s5){
cout<<"s5 <= s5"<<endl;
}
s6 = s4 + s5;
cout<<"s6 = s4 + s5 ="<<s6<<endl;
cout<<"s6[0]="<<s6[0]<<endl;
cout<<("cin>>s6")<<endl;
cin>>s6;
cout<<"s6="<<s6<<endl;
cout<<("getline(cin,s6)")<<endl;
getline(cin,s6);
cout<<"s6="<<s6<<endl;
myString s7("hello ");
myString s8("aaa");
cout<<"s7="<<s7<<endl;
cout<<"s8="<<s8<<endl;
cout<<"s7+=s8"<<endl;
s7+=s8;
cout<<"s7="<<s7<<endl;
myString s9 = "123";
myString s10 = "234";
cout<<"s9="<<s9<<endl;
cout<<"s10="<<s10<<endl;
cout<<"s9+ 222 ="<<s9+" 222"<<endl;
s9+=" 222";
cout<<"s9+= 222 ="<<s9<<endl;
s9+=s10;
cout<<"s9+=s10 ="<<s9<<endl;
return 0;
}
无参构造:
s0=
有参构造:
s1=hello
拷贝构造:
s2=hello
判空+拷贝赋值+size+c_str+at:
s3=hello
s3.size()=5
s3.at(1)=e
运算符重载:
s3 == s3
s3 != s2
s4=hello aaa
s5=hello bbb
s5 > s4
s4 >= s4
s4 < s5
s5 <= s5
s6 = s4 + s5 =hello aaahello bbb
s6[0]=h
cin>>s6
AAAA
s6=AAAA
getline(cin,s6)
AA AA
s6=AA AA
s7=hello
s8=aaa
s7+=s8
s7=hello aaa
s9=123
s10=234
s9+ 222 =123 222
s9+= 222 =123 222
s9+=s10 =123 222234