數據結構系列均是本人個(gè)人搜集和寫(xiě)出來(lái)的,若有錯誤請大家批評指正。(有的是c++,有的是偽代碼)
1. String
class String
{
public:
String( const char * str = NULL );
String( const String & other);
~String();
String & operator=( const String & other);
private:
char * m_p;
};
String::String( const char * str )
{
if ( NULL == str )
{
m_p = new char[1];
if ( NULL != m_p )
{
*m_p = '\0';
}
}
else
{
m_p = new char[strlen(str)+1];
if ( NULL != m_p )
{
strcpy( m_p, str );
}
}
}
String::String( const String & other )
{
m_p = new char[strlen(other.m_p)+1];
if ( NULL != m_p )
{
strcpy( m_p, other.m_p );
}
}
String & String::operator=( const String & other )
{
if ( this != &other )
{
delete [] m_p;
m_p = new char[strlen(other.m_p)+1];
if ( NULL != m_p )
{
strcpy( m_p, other.m_p );
}
}
return *this;
}
String::~String()
{
delete [] m_p;
}
聯(lián)系客服