string与char*比较 ——why use string

stringchar*比较

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;
};

原文链接: https://www.cnblogs.com/bastard/archive/2012/01/07/2315885.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    string与char*比较 ——why use string

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/39897

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午4:33
下一篇 2023年2月8日 下午4:34

相关推荐