string类的简单实现

//------------------------------------------------------------------
//FileName : String.h
//Author : Younger
//description : Over write the string class
//Date : 2016/07/13
//Referent : http://blog.csdn.net/moxiaomomo/article/details/6411584
//-------------------------------------------------------------------
#ifndef _MyString_String_H_
#define _MyString_String_H_


#include <iostream>
#include <iomanip>
using namespace std;
namespace Str
{
class string
{
friend ostream& operator<<(ostream&,string&);//overload operator<<
friend ostream& operator>>(iostream&,string&);//overload operator>>
public:
string(const char* pData = NULL);//default construction
string(const string& str);//copy construction
~string(){delete[] m_pData;}

string& operator+(const string& str);//overload operator+
string& operator=(const string& str);//overload operator=
string& operator+=(const string& str);//overload operator+=
bool operator==(const string& );//overload operator==
char& operator[](unsigned int)const;//overload operator[]

size_t size(){return strlen(m_pData);}
const char* c_str()const;
void append(const string& str);


private:
char* m_pData;	
};


inline void string::append(const string& str)
{
if (&str != NULL)
{
if (str.m_pData != NULL)
{
char* Tp = m_pData;
m_pData = new char[strlen(Tp)+strlen(str.m_pData)+1];
strcpy(m_pData,Tp);
strcat(m_pData,str.m_pData);
delete Tp;
Tp = NULL;
}
}
}


inline const char* string::c_str()const
{
return m_pData;
}


inline string& string::operator+=(const string& str)
{
if (str.m_pData != NULL)
{
if (m_pData == NULL)
{
m_pData = new char[strlen(str.m_pData)+1];
strcpy(m_pData,str.m_pData);
}
else 
{
char* tp = m_pData;
m_pData = new char[strlen(tp)+strlen(str.m_pData)+1];
strcpy(m_pData,tp);
strcat(m_pData,str.m_pData);
delete tp;
tp = NULL;
}
}
return *this;
}


inline string::string(const char* pData)
{
if (!pData)
{
m_pData = NULL;
}
else
{
m_pData = new char[strlen(pData)+1];
strcpy(m_pData,pData);
}
}


inline string::string(const string& str)
{
if (str.m_pData == NULL)
{
m_pData = NULL;
}
else
{
m_pData = new char[strlen(str.m_pData)+1];
strcpy(m_pData,str.m_pData);
}
}


inline string& string::operator=(const string& str)
{
if (this != &str)
{
delete m_pData;
if (str.m_pData != NULL)
{
m_pData = new char[strlen(str.m_pData)+1];
strcpy(m_pData,str.m_pData);
}
else
{
m_pData = NULL;
}
} 
return *this;
}


inline bool string::operator==(const string& str)
{
if(strlen(str.m_pData) != strlen(m_pData))
{
return false;
}
else if (strcmp(m_pData,str.m_pData))
{
return false;
}
return true;
}


inline string& string::operator+(const string& str)
{


static string Rstring;
if (str.m_pData == NULL)
{
Rstring.m_pData = m_pData;
}
else if (m_pData == NULL)
{
 
Rstring = str;
}
else 
{
Rstring.m_pData = new char[strlen(m_pData)+strlen(str.m_pData)+1];
strcpy(Rstring.m_pData,m_pData);
strcat(Rstring.m_pData,str.m_pData);
}
return Rstring;
}


inline char& string::operator[](unsigned int num)const
{
if (num >= 0 && num <= strlen(m_pData))
{
return m_pData[num];
} 
}


ostream& operator<<(ostream& os,string& str)
{
os<<str.m_pData;
return os;
}


istream& operator>>(istream& io,string& str)
{
char IoData[255];
io>>setw(255)>>IoData;
str = IoData;
return io;
}
}
#endif

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值