簡單字串 class
2008-11-14 15:10:15 發表
這只是一個簡單的字串class
只有必要的member function
跟過載 operator+ operator+=
file name : sstring.h
// compiler : vc++ 6.0
// date : 2008/11/14
// sample string
// only have += operator , + operator
#ifndef _SString_H_
#define _SString_H_
#include "iostream"
#include "stdlib.h" // 會掉字 所以用 "
class SString
{
public:
SString(); // default construct
SString(const SString& a_strR); // copy construct
SString(const char* a_zR); //轉換C style 的construct
~SString(); // destruct
void Swap(SString& a_strR); // 將兩個字串交換
SString& operator+=(const SString& a_strR);
const char* CStr() const; // 轉換成C style 的字串
SString& operator=(const SString& a_strR);
private:
SString(size_t n); // construnct and alloc n size, don't for user use
char *m_zStr;
size_t m_nLen;
};
// 紅色部份為一定要寫的member function 否則程式一定會不正常
// 兩字串相加
inline SString operator+(const SString& a_strL,const SString& a_strR)
{
SString strTmp(a_strL);
strTmp+=a_strR;
return strTmp;
}
inline std::ostream& operator<<(std::ostream& o,const SString r)
{
o<< r.CStr();
return o;
}
#endif // of _SString_H_
file name : sstring.cpp
#include "iostream"
#include "sstring.h"
#include "string.h"
SString::SString() : m_zStr(new char[1]),m_nLen(0) // default construct
{
m_zStr[0]=NULL;
}
SString::SString(const SString& a_strR) // copy construct
{
m_nLen=a_strR.m_nLen;
m_zStr=new char[m_nLen+1];
memcpy(m_zStr,a_strR.m_zStr,m_nLen+1);
}
SString::SString(const char* a_zR) //轉換C style 的construct
{
m_nLen=strlen(a_zR);
m_zStr=new char[m_nLen+1];
memcpy(m_zStr,a_zR,m_nLen+1);
}
SString::SString(size_t n) // construnct and alloc n size, don't for user use
{
m_nLen=n;
m_zStr=new char[n+1];
m_zStr[0]=NULL;
}
SString::~SString() // destruct
{
delete [] m_zStr;
m_zStr=NULL;
m_nLen=0;
}
void SString::Swap(SString& a_strR) // 將兩個字串交換
{
size_t nT=a_strR.m_nLen;
char* zT=a_strR.m_zStr;
a_strR.m_nLen=m_nLen;
a_strR.m_zStr=m_zStr;
m_nLen=nT;
m_zStr=zT;
}
SString& SString::operator+=(const SString& a_strR)
{
SString strT(m_nLen+a_strR.m_nLen);
memcpy(strT.m_zStr,m_zStr,m_nLen);
memcpy(strT.m_zStr+m_nLen,a_strR.m_zStr,a_strR.m_nLen+1);
strT.Swap(*this);
return *this;
}
const char* SString::CStr() const
{
return m_zStr;
}
SString& SString::operator=(const SString& a_strR)
{
SString oTmp(a_strR);
oTmp.Swap(*this);
return *this;
}
file name : main.cpp // test code
#include "iostream"
#include "sstring.h"
int main(int argc,char argv[])
{
SString a="2000";
SString b="1000";
SString c;
c=c;
c+=a;
c+=c;
c+=b;
std::cout << c << std::endl;
std::cout << (b+a);
return 0;
}
只有必要的member function
跟過載 operator+ operator+=
file name : sstring.h
// compiler : vc++ 6.0
// date : 2008/11/14
// sample string
// only have += operator , + operator
#ifndef _SString_H_
#define _SString_H_
#include "iostream"
#include "stdlib.h" // 會掉字 所以用 "
class SString
{
public:
SString(); // default construct
SString(const SString& a_strR); // copy construct
SString(const char* a_zR); //轉換C style 的construct
~SString(); // destruct
void Swap(SString& a_strR); // 將兩個字串交換
SString& operator+=(const SString& a_strR);
const char* CStr() const; // 轉換成C style 的字串
SString& operator=(const SString& a_strR);
private:
SString(size_t n); // construnct and alloc n size, don't for user use
char *m_zStr;
size_t m_nLen;
};
// 紅色部份為一定要寫的member function 否則程式一定會不正常
// 兩字串相加
inline SString operator+(const SString& a_strL,const SString& a_strR)
{
SString strTmp(a_strL);
strTmp+=a_strR;
return strTmp;
}
inline std::ostream& operator<<(std::ostream& o,const SString r)
{
o<< r.CStr();
return o;
}
#endif // of _SString_H_
file name : sstring.cpp
#include "iostream"
#include "sstring.h"
#include "string.h"
SString::SString() : m_zStr(new char[1]),m_nLen(0) // default construct
{
m_zStr[0]=NULL;
}
SString::SString(const SString& a_strR) // copy construct
{
m_nLen=a_strR.m_nLen;
m_zStr=new char[m_nLen+1];
memcpy(m_zStr,a_strR.m_zStr,m_nLen+1);
}
SString::SString(const char* a_zR) //轉換C style 的construct
{
m_nLen=strlen(a_zR);
m_zStr=new char[m_nLen+1];
memcpy(m_zStr,a_zR,m_nLen+1);
}
SString::SString(size_t n) // construnct and alloc n size, don't for user use
{
m_nLen=n;
m_zStr=new char[n+1];
m_zStr[0]=NULL;
}
SString::~SString() // destruct
{
delete [] m_zStr;
m_zStr=NULL;
m_nLen=0;
}
void SString::Swap(SString& a_strR) // 將兩個字串交換
{
size_t nT=a_strR.m_nLen;
char* zT=a_strR.m_zStr;
a_strR.m_nLen=m_nLen;
a_strR.m_zStr=m_zStr;
m_nLen=nT;
m_zStr=zT;
}
SString& SString::operator+=(const SString& a_strR)
{
SString strT(m_nLen+a_strR.m_nLen);
memcpy(strT.m_zStr,m_zStr,m_nLen);
memcpy(strT.m_zStr+m_nLen,a_strR.m_zStr,a_strR.m_nLen+1);
strT.Swap(*this);
return *this;
}
const char* SString::CStr() const
{
return m_zStr;
}
SString& SString::operator=(const SString& a_strR)
{
SString oTmp(a_strR);
oTmp.Swap(*this);
return *this;
}
file name : main.cpp // test code
#include "iostream"
#include "sstring.h"
int main(int argc,char argv[])
{
SString a="2000";
SString b="1000";
SString c;
c=c;
c+=a;
c+=c;
c+=b;
std::cout << c << std::endl;
std::cout << (b+a);
return 0;
}



