美文网首页
39_字符串类的创建(上)

39_字符串类的创建(上)

作者: 编程半岛 | 来源:发表于2018-07-13 20:44 被阅读7次

关键词:

0. 历史遗留问题

  • C语言不支持真正意义上的字符串
  • C语言用字符数组一组函数实现字符串操作
  • C语言不支持自定义类型,因此无法获得字符串类型
  • 从C语言到C++的进化过程引入了自定义类型
  • 从C++中可以通过类完成字符串类型的定义

1. DTLib中字符串类的设计

字符串类

2. 字符串类的实现

实现时的注意事项:

  • 无缝实现String对象与char*字符串的互操作
  • 操作符重载函数需要考虑是否支持const版本
  • 通过C语言中的字符串函数实现String的成员函数
    DTString.h
#ifndef DTSTRING_H
#define DTSTRING_H

#include <cstring>
#include <cstdlib>
#include "Object.h"
#include "Exception.h"

using namespace std;

namespace DTLib
{

class String : public Object
{
protected:
    char* m_str;
    int m_length;

    void init(const char* s);
public:
    String();
    String(char c);
    String(const char* s);
    String(const String& s);
    int length() const;
    const char* str() const;

    /* 操作符重载相关函数 */
    bool operator == (const String& s) const;
    bool operator == (const char* s) const;
    bool operator != (const String& s) const;
    bool operator != (const char* s) const;
    bool operator > (const String& s) const;
    bool operator > (const char* s) const;
    bool operator < (const String& s) const;
    bool operator < (const char* s) const;
    bool operator >= (const String& s) const;
    bool operator >= (const char* s) const;
    bool operator <= (const String& s) const;
    bool operator <= (const char* s) const;

    String operator + (const String& s) const;
    String operator + (const char* s) const;
    String& operator += (const String& s);
    String& operator += (const char* s);

    String& operator = (const String& s);
    String& operator = (const char* s);
    String& operator = (char c);


    ~String();
};

void String::init(const char *s)
{
    m_str = strdup(s);      // 复制一份字符串给m_str

    if( m_str )
    {
        m_length = strlen(m_str);
    }
    else
    {
        THROW_EXCEPTION(NoEnoughMemoryExcetion, "no memory to create String object...");
    }
}

String::String()
{
    init("");
}

String::String(char c)
{
    char s[] = {c, '\0'};

    init(s);
}

String::String(const char* s)
{
    init(s ? s : "");
}

String::String(const String& s)
{
    init(s.m_str);
}

int String::length() const
{
    return m_length;
}

const char* String::str() const
{
    return m_str;
}

bool String::operator == (const String& s) const
{
    return (strcmp(m_str, s.m_str) == 0);
}

bool String::operator == (const char* s) const
{
    return (strcmp(m_str, s ? s : "") == 0);
}

bool String::operator != (const String& s) const
{
    return !(*this == s);
}

bool String::operator != (const char* s) const
{
    return !(*this == s);
}

bool String::operator > (const String& s) const
{
    return (strcmp(m_str, s.m_str) > 0);
}

bool String::operator > (const char* s) const
{
    return (strcmp(m_str, s ? s : "") > 0);
}

bool String::operator < (const String& s) const
{
    return (strcmp(m_str, s.m_str) < 0);
}

bool String::operator < (const char* s) const
{
    return (strcmp(m_str, s ? s : "") < 0);
}

bool String::operator >= (const String& s) const
{
    return (strcmp(m_str, s.m_str) >= 0);
}

bool String::operator >= (const char* s) const
{
    return (strcmp(m_str, s ? s : "") >= 0);
}

bool String::operator <= (const String& s) const
{
    return (strcmp(m_str, s.m_str) <= 0);
}

bool String::operator <= (const char* s) const
{
    return (strcmp(m_str, s ? s : "") <= 0);
}

String String::operator + (const String& s) const
{
    return (*this + s.m_str);
}

String String::operator + (const char* s) const
{
    String ret;

    int len = m_length + strlen(s ? s : "");
    char* str = reinterpret_cast<char*>(malloc(len + 1));

    if( str )
    {
        strcpy(str, m_str);
        strcat(str, s ? s : "");

        free(ret.m_str);

        ret.m_str = str;
        ret.m_length = len;
    }
    else
    {
        THROW_EXCEPTION(NoEnoughMemoryExcetion, "No Memory to add String value...");
    }

    return ret;
}

String& String::operator += (const String& s)
{
    return (*this = *this + s);
}

String& String::operator += (const char* s)
{
    return (*this = *this + s);
}

String& String::operator = (const String& s)
{
    return (*this = s.m_str);
}

String& String::operator = (const char* s)
{
    if( m_str != s )
    {
        char* str = strdup(s ? s : "");

        if( str != NULL )
        {
            free(m_str);

            m_str = str;
            m_length = strlen(m_str);
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryExcetion, "No memory to assign a new String value...");
        }
    }

    return *this;
}

String& String::operator = (char c)
{
    char s[] = {c , '\0'};

    return (*this = s);
}


String::~String()
{
    free(m_str);
}

}

#endif // DTSTRING_H

3. 小结

  • C/C++语言本身不支持字符串类型
  • C语言通过字符数组一组函数支持字符串操作
  • C++通过自定义字符串类型支持字符串操作
  • 字符串类型通过C语言中的字符串函数实现

声明:此文章仅是本人在学习狄泰学院《数据结构实战开发教程》所做的笔记,文章中包含狄泰软件资料内容,一切版权归狄泰软件所有!
实验环境:ubuntu10 + Qt Creator2.4.1 + Qt SDK 4.7.4

相关文章

  • 39_字符串类的创建(上)

    关键词: 0. 历史遗留问题 C语言不支持真正意义上的字符串 C语言用字符数组和一组函数实现字符串操作 C语言不支...

  • Java——API知识

    1、String类 -----String类描述的是文本字符串序列,用于操作和创建字符串。 -----创建Stri...

  • Java实战开发篇-7 基础类库

    基础类库 一、String类 String类是定义不可变的字符串,如 用String类创建常量字符串以及相关子类 ...

  • iOS开发之NSString字符串和数组NSArray操作

    1、创建字符串对象数组 2、可变的字符串类 3、字典加数组操作 一、NSString 1、创建常量字符串。NSSt...

  • Java入门:String类

    Java String类 创建字符串String str = "Hello";字符串长度String类有一个访问器...

  • java中String常用方法总结

    在 Java 中字符串属于对象,用 String 类来创建和操作字符串。String类为final类,不能被继承。...

  • Java--String、StringBuffer 、Strin

    [1] String类 在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串。 [...

  • swift如何根据字符串,即类名创建类

    swift如何根据字符串,即类名创建类 申明类:required override init() 必须实现

  • 03.对象操作

    字符串的声明 在OC中,用NSString类声明的字符串时一个对象 用@创建字符串 创建一个格式化的字符串 字符串...

  • String类

    String类 String类描述的是文本字符串序列。 特点: 字符串是常量,它的值在创建之后不能再修改。 字符串...

网友评论

      本文标题:39_字符串类的创建(上)

      本文链接:https://www.haomeiwen.com/subject/oeqspftx.html