字符串类String
生活随笔
收集整理的這篇文章主要介紹了
字符串类String
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1 字符串類的初步設(shè)計(jì)
- 1.1 歷史遺留問題
- 1.2 字符串類的初步設(shè)計(jì)
- 1.3 代碼實(shí)現(xiàn)
- 2 字符串類功能的進(jìn)一步實(shí)現(xiàn)
- 2.1 重載數(shù)組訪問操作符[]
- 2.2 判斷是否以指定字符串開始或結(jié)束
- 2.3 在指定位置處插入字符串
- 2.4 去掉字符串兩端的空白字符
1 字符串類的初步設(shè)計(jì)
1.1 歷史遺留問題
- C語言不支持真正意義上的字符串。
- C語言用字符數(shù)組和一組函數(shù)實(shí)現(xiàn)字符串操作。
- C語言不支持自定義類型,因此無法獲得字符串類型。
- 從C到C++的進(jìn)化過程中引入了自定義類型,在C++中可以通過類完成字符串類型的定義。
1.2 字符串類的初步設(shè)計(jì)
類的聲明如下:
實(shí)現(xiàn)時(shí)的注意事項(xiàng):
- 無縫實(shí)現(xiàn)String對(duì)象與char*字符串的互操作。
- 操作符重載函數(shù)需要考慮是否支持const操作。
- 通過C語言中的字符串函數(shù)實(shí)現(xiàn)String的成員函數(shù)。
1.3 代碼實(shí)現(xiàn)
String.h:
#ifndef LSTRING_H #define LSTRING_H#include "Object.h" #include "Exception.h"namespace LemonLib {#define STR(str) (str ? str : "")class String : public Object { protected:char* m_str;int m_length;void init(const char* str); public:String();String(const char* str);String(char c);String(const String& s);String& operator = (const char* str);String& operator = (const String& s);String& operator = (char c);bool operator == (const char* str) const;bool operator == (const String& s) const;bool operator != (const char* str) const;bool operator != (const String& s) const;bool operator > (const char* str) const;bool operator > (const String& s) const;bool operator >= (const char* str) const;bool operator >= (const String& s) const;bool operator < (const char* str) const;bool operator < (const String& s) const;bool operator <= (const char* str) const;bool operator <= (const String& s) const;String operator + (const char* str) const;String operator + (const String& s) const;String& operator += (const char* str);String& operator += (const String& s);int length() const;const char* str() const;~String(); }; }#endif // STRING_HString.cpp:
#include "LString.h" #include <cstring> #include <cstdlib>using namespace std;namespace LemonLib { void String::init(const char* str) {m_str = strdup(STR(str));if (m_str != NULL){m_length = strlen(m_str);}else{THROW_EXCEPTION(NoEnoughMemoryException, "string init error, no enough memory");} }String::String() {init(""); }String::String(const char* str) {init(str); }String::String(char c) {char str[] = {c, '\0'};init(str); }String::String(const String& s) {init(s.m_str); }String& String::operator = (char c) {char str[] = {c, '\0'};return (*this = str); }String& String::operator = (const char* str) {if (m_str != str){free(m_str);init(str);}return *this; }String& String::operator = (const String& s) {return (*this = s.m_str); }int String::length() const {return m_length; }const char* String::str() const {return m_str; }bool String::operator == (const char* str) const {return strcmp(m_str, str) == 0; }bool String::operator == (const String& s) const {return (*this == s.m_str); }bool String::operator != (const char* str) const {return !(*this == str); }bool String::operator != (const String& s) const {return !(*this == s); }bool String::operator > (const char* str) const {return strcmp(m_str, STR(str)) > 0; }bool String::operator > (const String& s) const {return (*this > s.m_str); }bool String::operator >= (const char* str) const {return strcmp(m_str, STR(str)) >= 0; }bool String::operator >= (const String& s) const {return (*this >= s.m_str); }bool String::operator < (const char* str) const {return strcmp(m_str, str) < 0; }bool String::operator < (const String& s) const {return (*this < s.m_str); }bool String::operator <= (const char* str) const {return strcmp(m_str, STR(str)) <= 0; }bool String::operator <= (const String& s) const {return (*this <= s.m_str); }String String::operator + (const char* str) const {int len = m_length + strlen(STR(str));String ret;char* p = reinterpret_cast<char*>(malloc(len + 1));if (p != NULL){strcpy(p, m_str);strcat(p, STR(str));free(ret.m_str);ret.m_str = p;ret.m_length = len;return ret;}else{THROW_EXCEPTION(NoEnoughMemoryException, "string operator + error, no enough memory");} }String String::operator + (const String& s) const {return (*this + s.m_str); }String& String::operator += (const char* str) {return (*this = *this + str); }String& String::operator += (const String& s) {return (*this += s.m_str); }String::~String() {free(m_str); } }2 字符串類功能的進(jìn)一步實(shí)現(xiàn)
接下來主要實(shí)現(xiàn)如下函數(shù):
2.1 重載數(shù)組訪問操作符[]
函數(shù)聲明:
char& operator [] (int i); char operator [] (int i) const;注意事項(xiàng):
- 當(dāng)i的取值不合法時(shí),拋出異常:
- 合法范圍:(0 <= i) && (i < m_length)
代碼實(shí)現(xiàn)如下:
char& String::operator [] (int i) {if ((0 <= i) && (i < m_length)){return m_str[i];}else{THROW_EXCEPTION(IndexOutOfBoundsException, "string operator [] error, out index");} }char String::operator [] (int i) const {return (const_cast<String&>(*this))[i]; }2.2 判斷是否以指定字符串開始或結(jié)束
函數(shù)聲明:
bool startWith(const char* s) const; bool startWith(const String& s) const; bool endOf(const char* s) const; bool endOf(const String& s) const;示意圖:
代碼實(shí)現(xiàn):
2.3 在指定位置處插入字符串
函數(shù)聲明:
String& insert(int i, const char* s); String& insert(int i, const String& s);示意圖:
代碼實(shí)現(xiàn):
2.4 去掉字符串兩端的空白字符
函數(shù)聲明:
String& trim();示意圖:
代碼實(shí)現(xiàn):
總結(jié)
以上是生活随笔為你收集整理的字符串类String的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QueueToStack
- 下一篇: 烫伤后能涂牙膏酱油什么的吗?