STL:string类的简单实现

本文详细介绍了自定义string类的设计与实现过程,包括构造函数、析构函数、拷贝构造函数、赋值函数、字符串连接及长度获取等功能,并对常见错误进行了修正。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读书笔记: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.asm
main_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;
	}
}

编译运行,结果可行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值