一 string與char*比較
1 string是一個類,char*是一個指向char型的指針。
string封裝了char*,管理這個字符串封裝了char*,是一個char*型的容器,使用靈活性強便於功能擴展。
2 不用考慮內存釋放和越界
String封裝了char*,負責管理char*字符串,管理為char*所分配的內存。
每一次string的復制,取值都由string類負責維護,不用擔心復制越界和取值越界等。
3 string支持運算
重載運算符:
string a;string b;string c;
相加運算: c = a + b ;
比較運算:a > b, a != b;
4 能提供系列字符串操作函數
查找find,拷貝copy,刪除delete
替換replace,插入insert
……
5 支持擴展
使用靜態內存或者動態內存
使用資源ID加載字符串
操作安全使用鎖
最本質的區別就是:string是一個類,char是基本類型,string封裝了char。
二 string的實現和應用
C++標准庫STL里面提供string的實現。
這里看一下我工作平台中string的實現方式,感覺寫的還不錯,值得學習。
class String
{
public:
// Default constructor
String() : m_bufType(BUF_TYPE_NONE), m_buf(NULL)
{
}
// Initialized with resource ID
String(const U32 id);
// Copy constructor
String(const String &other) :
m_bufType(BUF_TYPE_NONE),
m_buf(NULL)
{
assignWith(other);
}
// Destructor
~String()
{
//保證分配的內存能夠被釋放
clear();
}
// Operator基本的運算操作 [],=,*,==,+=,……
public:
// RETURNS: The character at given position index
WChar operator [] (S32 index) const
{
return getItem(index);
}
// RETURNS: The buffer of the string
String &operator =(const String &other);
// RETURNS: The buffer of the string.
operator const WChar *() const
{
return m_buf;
}
// RETURNS: Return VFX_TRUE the string is equal to the other.
Bool operator ==(const String &other) const
{
return compareWith(other) == 0;
}
// RETURNS: Return VFX_TRUE the string is *not* equal to the other.
Bool operator !=(const String &other) const
{
return compareWith(other) != 0;
}
// RETURNS: Reference to the string
String &operator +=(const String &other)
{
appendWith(other);
return *this;
}
// Method
public:
// RETURNS: The character at given position index
WChar getItem(
S32 index // [IN] The position index to get
) const
{
return m_buf[index];
}
// SEE ALSO: unlockBuf
WChar *lockBuf(
VfxU32 initialLength // [IN] The initial buffer length, included zero terminal.
);
// SEE ALSO: lockBuf
void unlockBuf();
// RETURNS: The buffer of the string
const WChar *getBuf() const
{
return m_buf;
}
// RETURNS: The charactor numbers of the string.
U32 getLength() const;
// RETURNS: Return VFX_TRUE if the string is empty or NULL
Bool isEmpty() const
{
return m_buf ? (m_buf[0] == 0) : VFX_TRUE;
}
// RETURNS: Reference to the string
String &setEmpty();
// RETURNS: Return VFX_TRUE if the string is NULL
VfxBool isNull() const
{
return m_buf == NULL;
}
// RETURNS: Reference to the string
String &setNull()
{
clear();
return *this;
}
// RETURNS: Return VFX_TRUE if the string buffer is dynamic
Bool isDynamic() const
{
return VFX_FLAG_HAS(m_bufType, BUF_TYPE_DYNAMIC);
}
// RETURNS: Reference to the string
String &loadFromRes(ResId res_id);
// RETURNS: Reference to the string
String &loadFromMem(const WChar *mem);
// RETURNS: Reference to the string
String &format(const WChar *format, ...);
// RETURNS: Reference to the string
String &format(const WChar *format, ...);
protected:
const WChar *cloneBuf(const WChar *buf);
// Implementation
private:
BufTypeEnum m_bufType;
const WChar *m_buf;
// Helper methods
void clear();
void assignWith(const String &ohter);
void appendWith(const String &ohter);
S32 compareWith(const String &str) const;
};