zoukankan      html  css  js  c++  java
  • 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;
    };



  • 相关阅读:
    基于properties文件的Spring Boot多环境切换
    在mysql命令行下执行sql文件
    thymeleaf th:href 多个参数传递格式
    Mybatis 级联查询时只查出了一条数据
    IDEA技巧-自动导包(Auto import)以及自动优化包
    Drools学习教程
    Drools学习教程
    Drools学习教程
    Drools学习教程
    一道有趣的类加载面试题
  • 原文地址:https://www.cnblogs.com/bastard/p/2315885.html
Copyright © 2011-2022 走看看