String深拷贝+String增删改查

#include<iostream>
#include<assert.h>
#include<string.h>
using namespace std;

#define DEFAULT_CAPACITY 5
class String
{
public:
	String()
		:_str(new char[DEFAULT_CAPACITY])
		,_size(0)
		,_capacity(DEFAULT_CAPACITY)
	{
		_str[0] = '\0';
	}
	String(const char* str)
		:_size(strlen(str))
		,_capacity(_size + DEFAULT_CAPACITY)
	{
		_str = new char[_capacity];
		strcpy(_str, str);
	}
	~String()
	{
		if (_str)
		{
			delete[] _str;
		}
	}
	void Swap(String&s)
	{
		swap(_str,s._str);
		swap(_capacity,s._capacity);
		swap(_size,s._size);
	}
	String(const String& s)
		:_str(NULL)
		, _size(0)
		, _capacity(0)
	{
		String tmp(s._str);
		this->Swap(tmp);
	}
	String& operator=(String& s)
	{
		this->Swap(s);
		return *this;
	}
public:
	const char* GetStr()
	{
		return _str;
	}
	void _CheckCapacity(size_t capacity)//扩容
	{
		if (_capacity<capacity)
		{
			_capacity *= capacity;
			_capacity += DEFAULT_CAPACITY;
			char* tmp = new char[_capacity];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
		}
	}
	void PushBack(char ch)
	{
		_CheckCapacity(_size + 2);
		_str[_size++] = ch;
		_str[_size] = '\0';
	}
	void PopBack()
	{
		if (_size>0)
		{
			_str[--_size] = '\0';
		}
	}
	void Insert(size_t pos,char ch)
	{
		assert(pos<=_size);
		_CheckCapacity(_size + 2);
		for (size_t i = _size; i >= pos; --i)
		{
			_str[i + 1] = _str[i];
		}
		_str[pos] = ch;
		++_size;
	}
	void Insert(size_t pos,const char* str)
	{
		assert(pos<=_size);
		int len = strlen(str);
		_CheckCapacity(_size + len+1);
		for (int i = _size; int j = _size + len; i >= (int)pos)
		{
			_str[j--] = _str[i--];
		}
		while (*str)
		{
			_str[pos++] = *str++;
		}
		_size += len;
		_str[_size] = '\0';
	}
	int Find(char* str)
	{
		char* src = _str;
		char* sub = str;
		int index=0;
		while (*src&&*sub)
		{
			while (*src==*sub)
			{
				src++;
				sub++;
			}
			if (*sub == '\0')
			{
				return index;
			}
			else
			{
				++index;
				sub = src;
				src++;
			}
		}
		return -1;
	}
public:
	bool operator<(const String& s)
	{
		const char* s1 = _str;
		const char* s2 = s._str;
		while (*s1 && *s2)
		{
			if (*s1 < *s2)
			{
				return true;
			}
			else if (*s1 > *s2)
			{
				return false;
			}
			else
			{
				++s1;
				++s2;
			}
		}
		if (*s1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool operator>(const String&s)
	{
		return !(*this < s || *this == s);
	}
	bool operator<=(const String&s)
	{
		return !(*this >s);
	}
	bool operator>=(const String&s)
	{
		return !(*this <s);
	}
	bool operator==(const String&s)
	{
		const char* s1 = _str;
		const char* s2 = s._str;
		while (*s1 && *s2)
		{
			if (*s1++ != *s2++)
			{
				return false;
			}
		}
			if (*s1 == *s2)
			{
				return true;
			}
			else
			{
				return false;
			}
	}
	String operator+(const String&s)
	{
		/*String tmp(_str);
		char* str = s._str;
		while (*str)
		{
			tmp.PushBack(*str++);
		}*/
		String tmp(_str);
		tmp.Insert(_size, s._str);
		return tmp;
	}
	String& operator+=(const String&s)
	{
		this->Insert(_size,s._str);
		return *this;
	}

	friend ostream& operator<<(ostream& os, const String& s);
private:
	size_t _size;//大小
	size_t _capacity;//容量
	char* _str;//指向字符串的指针
};
ostream& operator<<(ostream& os, const String& s)
{
	os << s._str;
	return os;
}
void Test1()
{
	String s1("qwer");
	s1.PushBack('t');
	cout << "s1:" << s1 << endl;
}
void Test2()
{
	String s1("qwer");
	String s2(s1);

	cout << "s1==s2 ?" << (s1 == s2) << endl;
}
int main()
{
	Test1();
	//Test2();
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值