STL:string类的简单实现

来源:互联网 发布:淘宝拍卖能捡漏吗 编辑:程序博客网 时间:2024/06/05 22:58

读书笔记:string类的实现

在看《后台开发核心技术与应用实践》string类的实现时,对理解string很有帮助,特此总结:

 1、首先string类的定义原型代码:

class String{public:String(const char *str = NULL);//构造函数String(const String &other);//拷贝构造函数~String();//析构函数String &operator =(const String &other);//赋值函数String &operator +(const String &other);//字符串连接bool operator == (const String &other);//判断相等int getLength();//返回长度public:char *m_data;//保存字符串};

string类其实是对一个字符串指针进行一系列操作的类,也就是说string类底层是一个字符串指针。

2,成员函数:

(1),构造函数String::String(const char *str){if(str==NULL){m_data = new char[1];m_data = '\0';}else{int length = strlen(str);m_data = new char[length+1];strcpy(m_data,str);}}

传入的是一个char*类型的字符串,如果传入的str是个空的字符串直接用'\0'赋值,否则为m_data预留length+1长度,其中+1是用来存放'\0'。

(2),析构函数String::~String(){if(m_data){delete []m_data;m_data=NULL;}}
析构函数的主要功能主要是删除成员变量,需要先判断字符指针是否为空,如果不为空则将其删除,并且将其指针指向null
(3),拷贝构造函数String::String(const String &other){if(!other.m_data){m_data=0;}else{m_data = new char[strlen(other.m_data)+1];strcpy(m_data,other.m_data);}}

拷贝构造函数里需要注意的是,传入的参数是个常引用,这样可以不用新增一个栈变量和参数内容可以保持不变,不被修改

(4),赋值函数String & String::operator =(const String &other){if(this!=&other){delete[]m_data;if(!other.m_data){m_data = 0;}else{m_data = new char [strlen(other.m_data)+1];strcpy(m_data,other.m_data);}}return *this;}

赋值函数需要注意的是,如果传入的参数内容与本身内容一致,无需赋值。否则先清空本身内容。

(5),字符串连接String & String::operator +(const String &other){String newString;if(!other.m_data){newString = *this;}else if(!m_data){newString = other;}else{newString.m_data = new char[strlen(m_data)+strlen(other.m_data)+1];strcpy(newString.m_data,m_data);strcat(newString.m_data,other.m_data);}return newString;

分三种情况进行操作。

(6),判断相等bool String::operator ==(const String &other){if(strlen(m_data)!=strlen(other.m_data)){return false;}else{return strcmp(m_data,other.m_data)?false:true;}}

(7),返回长度int String::getLength(){return strlen(m_data);}

在编程过程中,(5),字符串的连接一直报错:
#strlen.asmmain_loop:        mov     eax,dword ptr [ecx]     ; read 4 bytes           mov     edx,7efefeffh        add     edx,eax        xor     eax,-1        xor     eax,edx        add     ecx,4        test    eax,81010100h        je      short main_loop        ; found zero byte in the loop        mov     eax,[ecx - 4]        test    al,al                   ; is it byte 0        je      short byte_0        test    ah,ah                   ; is it byte 1        je      short byte_1        test    eax,00ff0000h           ; is it byte 2        je      short byte_2        test    eax,0ff000000h          ; is it byte 3        je      short byte_3        jmp     short main_loop         ; taken if bits 24-30 are clear and bit                                        ; 31 is set
没有仔细去找原因,只是对(5)代码修改了一下:

String & String::operator +(const String &other){if(!other.m_data){return *this;}else if(!m_data){strcpy(m_data,other.m_data);return *this;}else{char *temp = new char [strlen(m_data) + strlen(other.m_data) +1];strcpy(temp,m_data);strcat(temp,other.m_data);m_data = new char[strlen(temp)+1];strcpy(m_data,temp);delete []temp;return *this;}}

编译运行,结果可行。


0 0
原创粉丝点击