String类的增删查改(深拷贝,现代写法)


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


//String类,深拷贝的增删查改
class String
{
public:
	String(char* str = "")
	{
		_size = strlen(str);
		_capacity = _size;	//不含'\0'
		_str = new char[_capacity + 1];
		strcpy(_str, str);
	}
	
	String(const String& s)
		:_str(NULL)
		,_size(0)
		,_capacity(0)
	{
		String tmp(s._str);	//现代写法
		Swap(tmp);
	}


	String& operator=(String s)
	{
		Swap(s);
		return *this;
	}


	~String()
	{
		if(_str)
		{
			delete[] _str;
			_str = NULL;
			_size = _capacity = 0;
		}
	}


	void Swap(String& s)
	{
		swap(_str, s._str);
		swap(_size, s._size);
		swap(_capacity, s._capacity);
	}


	char* GetStr()
	{
		return _str;
	}


	size_t Capacity()
	{
		return _capacity;
	}


	size_t Size()
	{
		return _size;
	}
	
	void PushBack(char ch) ;			//尾插字符
	void PushBack(const char* str) ;		//尾插串
	void PopBack() ;				//尾删字符
	void Insert(size_t pos, char ch) ;		//定位插入字符
	void Insert(size_t pos, const char* str);	//定位插入串
	void Erase(size_t pos, size_t count);		//定位删除count个字符


	size_t Find(char ch) const;			//查找字符下标
	size_t Find(const char* str) const;		//查找串下标
	
	char& operator[](size_t pos) ;			//按下标修改字符


	void Expand(size_t n) ;				//指定增容为n字节,不含'\0'


	bool operator<(const String& s) const;		//各种运算符重载
	bool operator<=(const String& s) const; 
	bool operator>(const String& s) const; 
	bool operator>=(const String& s) const; 
	bool operator==(const String& s) const; 
	bool operator!=(const String& s)const; 
	


private:
	char* _str;
	size_t _size;		//字符个数
	size_t _capacity;	//容量空间
};


void String::Expand(size_t n)
{
	if(n > _capacity)
	{
		_str = (char*)realloc(_str, n+1);
		assert(_str);
		_capacity = n;
	}
}


char& String::operator[](size_t pos)
{
	return _str[pos];
}


void String::PushBack(char ch)
{
	if(_capacity == _size)
	{
		Expand(_capacity * 2);
	}
	_str[_size++] = ch;
	_str[_size] = '\0';
}


void String::PushBack(const char* str)
{
	if( _size+strlen(str) > _capacity )
	{
		Expand(_size+strlen(str));
	}
	strcpy(_str+_size, str);
	_size += strlen(str);
}


void String::PopBack()
{
	assert(_size);
	--_size;
}


void String::Insert(size_t pos, char ch)
{
	if (_size == _capacity)
	{
		Expand(_capacity * 2);
	}
	//不要动_str,这是this指针会改值
	size_t end = _size;	
	while( end >= pos )
	{
		_str[end+1] = _str[end];
		--end;
	}
	_str[pos] = ch;
	++_size;
}


void String::Insert(size_t pos, const char* str)
{
	int len = strlen(str);
	if (_size + len > _capacity)
	{
		Expand(_size + len);
	}


	int end = _size;
	while(end >= (int)pos)
	{
		_str[end+len] = _str[end];
		--end;
	}
	
	while(*str)
	{
		_str[pos++] = *str++;
	}
	_size += len;
}


void String::Erase(size_t pos, size_t count)
{
	//1.删除的数据超出或等于原长度
	if(pos+count >= _size)
	{
		_str[pos] = '\0';
		_size = pos;
	}
	else
	{
		strcpy(_str+pos, _str+pos+count);
		_size -= count;
	}
}


size_t String::Find(char ch) const
{
	for(size_t i =0; i<_size; i++)
	{
		if(_str[i] == ch)
		{
			return i;
		}
	}
	return -1;
}


bool String::operator==(const String& s) const
{
	size_t i = 0;
	for(i=0; i<_size && i<s._size; i++)
	{
		if(_str[i] != s._str[i])
		{
			return false;
		}
	}
	if(i==_size && i==s._size)
		return true;
	else
		return false;
}


bool String::operator<(const String& s) const
{
	size_t i = 0;
	for(i=0; i<_size && i<s._size; i++)
	{
		if(_str[i] > s._str[i])
			return false;
		if(_str[i] < s._str[i])
			return true;
	}
	if(i==_size && i!=s._size)
		return true;
	else
		return false;
}


bool String::operator<=(const String& s) const
{
	return ((*this<s) || (*this == s));
}


bool String::operator>(const String& s) const
{
	return !((*this<s) || (*this == s));
}


bool String::operator>=(const String& s) const 
{
	return !(*this<s);
}


bool String::operator!=(const String& s)const 
{
	return !(*this == s);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值