C++ 写自己的string类(作风::懒)

本文介绍了一个用C++实现的自定义字符串类的设计与实现细节,包括构造函数、运算符重载等功能,并展示了如何使用这个类进行字符串操作。
#include <iostream>
#include<cstdlib>
#include <conio.h>
using namespace std;


class Cstring
{
private:
	unsigned int len;
	char *st;
	explicit Cstring(const unsigned int i)
	{
		len = 0;
		st = new char[i+1];
		return;
	}
public:
	~Cstring()
	{
		delete []st;
		st = 0;
	}
	explicit	Cstring()
	{
		len = 0;
		st = new char[len+1];
		st[0] = '\0';
	}
	Cstring(const Cstring &s)
	{
		len = s.len;
		st = new char[len + 1];
		for ( int i = 0; i < len; i++)
		{
			st[i] = s[i];
		}
		st[len] = '\0';
		return;
	}
	explicit Cstring(const char *const ch)
	{
		len = strlen(ch);
		st = new char[len + 1];
		for ( int i = 0; i < len; i++)
		{
			st[i] = ch[i];
		}
		st[len] = '\0';
		return;
	}
	explicit	Cstring(const char ch)
	{
		len = 1;
		st = new char[len + 1];
		st[0] = ch;
		st[1] = '\0';
		return;
	}
	char &operator[](unsigned int i)const
	{
		if (i > len - 1)
		{
			return st[len - 1];
		}
		else
			return st[i];
	}
	char &operator[](unsigned int i)
	{
		if (i > len - 1)
		{
			return st[len - 1];
		}
		else
			return st[i];
	}
	Cstring &operator=(const char * const ch)
	{
		if (strcmp(st, ch) == 0)
			return *this;
		char *p = st;
		delete []p;
		p = 0;
		len = strlen(ch);
		st = new char[len + 1];
		for (unsigned int i = 0; i < len; i++)
		{
			st[i] = ch[i];
		}
		st[len] = '\0';
		return *this;
	}
	Cstring &operator=(const Cstring &s)
	{
		if (this == &s)
			return *this;
		char *p = st;
		delete[]p;
		p = 0;
		len = s.len;
		st = new char[len + 1];
		for ( int i = 0; i < len; i++)
		{
			st[i] = s[i];
		}
		st[len] = '\0';
		return *this;
	}
	const	Cstring operator+(const Cstring &s)
	{
		unsigned int temlen = len + s.len;
		Cstring temp(temlen);
		for (unsigned int i = 0; i < len; i++)
		{
			temp.st[i] = st[i];
		}
		for (unsigned int j = 0; j < s.len; j++)
		{
			temp.st[len + j] = s.st[j];
		}
		temp.len = temlen;
		temp.st[temp.len] = '\0';
		return temp;
	}
	const	Cstring operator+(const char *const ch)
	{
		unsigned int templen = len + strlen(ch);
		Cstring temp(templen);
		for (unsigned int i = 0; i < len; i++)
		{
			temp.st[i] = st[i];
		}
		for (unsigned int j = 0; j < strlen(ch); j++)
		{
			temp.st[len + j] = ch[j];
		}
		temp.len = templen;
		temp.st[temp.len] = '\0';
		return temp;
	}

	friend ostream&operator<<(ostream &o, const Cstring &s)
	{
		o << s.st;
		return o;
	}
	friend istream&operator>>(istream &in,  Cstring &s)
	{
		char ch;
		int i = 0;
		while ((ch = cin.get())!='\n')
		{
			if (!i)
			{
				delete[]s.st;
				s.st = 0;
				s.len = i+1;
				s.st = (char*)realloc(s.st, (s.len+1)*sizeof(char));
				s.st[i] = ch;
				s.st[i+1] = '\0';
				i++;
			}
			else
			{
				s.len = i + 1;
				s.st = (char*)realloc(s.st, (s.len + 1)*sizeof(char));
				s.st[i] = ch;
				s.st[i + 1] = '\0';
				i++;
			}
		}
		return in;
	}

};

int main()
{
	Cstring s;
	s = "123333333";
	s[4] = '1';
	Cstring ss(s);
	cout << ss << endl;
	
	system("pause");
	return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值