mystring c++ 自己的string 封装

时间:2024-07-30 18:04:20
 1 /*************************************************************************
> File Name: mystring.h
> Author: lukey
> Mail: lukey123@foxmail.com
> Created Time: Wed 17 Jun 2015 08:50:49 PM CST
************************************************************************/ #ifndef __MYSTRING__
#define __MYSTRING__ class String
{
public:
String();
String(const char *);//有参构造函数
String(const String & rhs); //复制构造
~String(); String & operator=(const String & rhs);//赋值运算符的两种情况
String & operator=(const char *str); String & operator+=(const String & rhs);
String & operator+=(const char * str); char & operator[](std::size_t index);
const char & operator[](std::size_t index) const; std::size_t size() const;
const char* c_str() const;
void debug(); //String 类和char相加的几个情况
friend String operator+(const String & s1, const String & s2);
friend String operator+(const String &, const char *);
friend String operator+(const char *, const String &); friend bool operator==(const String &, const String &);
friend bool operator!=(const String &, const String &); friend bool operator<(const String &, const String &);
friend bool operator>(const String &, const String &);
friend bool operator<=(const String &, const String &);
friend bool operator>=(const String &, const String &); friend std::ostream & operator<<(std::ostream & os, const String &s);
friend std::istream & operator>>(std::istream & is, String & s); private:
char *pstr_;
}; #endif
 /*************************************************************************
> File Name: mystring.cc
> Author: lukey
> Mail: lukey123@foxmail.com
> Created Time: Wed 17 Jun 2015 09:18:55 PM CST
************************************************************************/ #include<iostream>
#include<cstring>
#include<stdlib.h>
#include"mystring.h"
using namespace std;
#if 0
class String
{
public:
private:
char *pstr_;
};
#endif //构造函数
String::String()
{
std::cout << "String()" << std::endl;
pstr_ = new char[];//new 已经初始化了
}
String::String(const char *str)//有参构造函数
{
std::cout << "String(const char * str)" << std::endl;
pstr_ = new char[strlen(str)+];
strcpy(pstr_, str);
}
String::String(const String & rhs) //复制构造,考虑自复制情况?
{
std::cout << "String(const String & rhs)" << std::endl;
pstr_ = new char[strlen(rhs.pstr_) + ];
strcpy(pstr_, rhs.pstr_);
}
String::~String()
{
std::cout << "~String()" << std::endl;
delete []pstr_;
} String & String::operator=(const String & rhs)//赋值运算符的两种情况,考虑自赋值情况
{
std::cout << "String & operator=(const String & rhs)" << std::endl;
if(this == &rhs)
return *this;
delete []pstr_;
pstr_ = new char[strlen(rhs.pstr_) + ];
strcpy(pstr_, rhs.pstr_);
return *this;
}
String & String::operator=(const char *str)
{
std::cout << "String & operator=(const char *str)" << std::endl;
pstr_ = new char[strlen(str) + ];
strcpy(pstr_, str);
return *this;
} String & String::operator+=(const String & rhs) //rhs连接到pstr_后面
{
std::cout << "operator+=(const String & rhs)" << std::endl;
int len = strlen(rhs.pstr_) + strlen(pstr_);
pstr_ = (char *)realloc(pstr_, len + );
strcat(pstr_, rhs.pstr_);
return *this;
}
String & String::operator+=(const char * str)
{
std::cout << "operator+=(const char * str)" << std::endl;
int len = strlen(str) + strlen(pstr_);
pstr_ = (char *)realloc(pstr_, len + );
strcat(pstr_, str);
return *this;
} //下标运算符,非常量,可以修改值
char & String::operator[](std::size_t index)
{
return pstr_[index];
} //常量对象取下标,不能为其赋值
const char & String::operator[](std::size_t index) const
{
return pstr_[index];
} //字符串容量
std::size_t String::size() const
{
return strlen(pstr_);
} //转换成c类型字符串,以'\0'结尾
const char* String::c_str() const
{
int len = strlen(pstr_); pstr_[len + ] = '\0';
return pstr_;
} //不懂?打印出字符串?
void String::debug()
{
std::cout << pstr_ << std::endl;
} String operator+(const String & s1, const String & s2)
{
std::cout << "operator+(const String & s1,const String & s2)" << std::endl;
String ret_str = s1.pstr_;
ret_str += s2.pstr_;
return ret_str;
} String operator+(const String & s, const char * str)
{
std::cout << "operator+(String, char *)" << std::endl;
String temp(str);
return (s + temp); //直接调用上面的(+)函数 } String operator+(const char * str, const String & s)
{
std::cout << "operator+( char *, String)" << std::endl;
String temp(str);
return (s + temp); //直接调用上面的(+)函数
} bool operator==(const String & lstr, const String & rstr)
{
std::cout << "==" << std::endl;
if(strcmp(lstr.pstr_, rstr.pstr_) == )
return true;
else
return false;
} bool operator!=(const String & lstr, const String & rstr)
{
std::cout << "!=" << std::endl;
return !(lstr == rstr);
} bool operator<(const String & lstr, const String & rstr)
{
std::cout << "<" << std::endl;
if(strcmp(lstr.pstr_, rstr.pstr_) < )
return true;
else
return false;
} bool operator>(const String & lstr, const String & rstr)
{
std::cout << ">" << std::endl;
if(strcmp(lstr.pstr_, rstr.pstr_) > )
return true;
else
return false;
}
bool operator<=(const String & lstr, const String & rstr)
{
std::cout << "<=" << std::endl;
if(strcmp(lstr.pstr_, rstr.pstr_) <= )
return true;
else
return false;
} bool operator>=(const String & lstr, const String & rstr)
{
std::cout << ">=" << std::endl;
if(strcmp(lstr.pstr_, rstr.pstr_) >= )
return true;
else
return false;
} std::ostream & operator<<(std::ostream & os, const String &s)
{
os << s.pstr_ << " ";
return os;
}
std::istream & operator>>(std::istream & is, String & s)
{
is >> s.pstr_;
return is; //貌似有坑, 目前不能输入空格
} //测试时每个函数调用都打印了信息
int main(void)
{
String s1("hello");
s1.debug();
std::cout << s1;
std::cout << std::endl;
String s2("world");
s2.debug();
if(s1 > s2)
std::cout << "s1 > s2" << std::endl; s1 = s2;
s1.debug();
String s3(s1);
s3.debug(); String s4 = s2 + s3;
s4.debug(); String s5;
std::cout << s5 << std::endl;
std::cin >> s5;
std::cout << s5 << std::endl;
return ;
}