39_字符串类的创建(上)
2018-07-13 本文已影响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