C++ String的增删查改


#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;

class String
{
public:
	String(char* str = "")
		:_str(new char[strlen(str)+1])
		,_size(strlen(str))
		,_capacity(strlen(str))
	{
		strcpy(_str, str);
	}

	String(const String& s)
		:_str(NULL)
		,_size(strlen(s._str))
		,_capacity(strlen(s._str))
	{
		String tmp(s._str);
		swap(_str, tmp._str);
	}

	String& operator =(String s)
	{
		swap(_str, s._str);
		_size = s._size;
		_capacity = s._capacity;
		return *this;
	}
//增删查改
	void PushBack(char ch)
	{
		GetExpand(_size+1);
		_str[_size] = ch;
		_str[++_size] = '\0';
	}

	void PushBack(const char* str)
	{
		GetExpand(_size+strlen(str));
		strcpy(_str+_size, str);
		_size += strlen(str);
		_str[_size] = '\0';
	}

	void PopBack()
	{
		_str[_size-1] = '\0';
		_size--;
	}

	void Insert(size_t pos, char ch)
	{
		GetExpand(_size+1);

		int end = _size;
		while(end >= (int)pos)//思考:这里是从'\0'开始移动的
		{
			_str[end+1] = _str[end];
			end--;
		}
		_str[pos] = ch;
		_size++;
	}

	void Insert(size_t pos, const char* str)
	{
		size_t len = strlen(str);
		GetExpand(_size + len);

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

	}

	void Erase(size_t pos, size_t count)
	{
		int start = pos;
		int end = pos + count;
		while(_str[end])
		{
			_str[start++] = _str[end++];
		}
		_str[start] = '\0';//这里的start已经是_str的最后位置
		_size -= count;
	}

	int Find(char ch)const 
	{
		int i = 0;
		while(_str[i] != '\0')
		{
			if(_str[i++] == ch)
				return i-1;
		}
		return -1;

	}

	int Find(const char* str)const
	{
		size_t len = strlen(str);
		size_t i = 0;
		char* src_index = _str;
		char* dest_index = (char*)str;
		char* tmp_index = NULL;

		while((_size-i) >= len)
		{
			src_index = _str + i;
			if(*src_index == *dest_index)
			{
				tmp_index = dest_index;
				while((*tmp_index++) == (*src_index++))
				{
					if((*src_index == '\0') || (*tmp_index == '\0'))
						return i;
				}
			}
			i++;
		}
		return -1;
	}

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

	bool operator >(const String& s)const
	{
		if(strcmp(_str, s._str) > 0)
			return true;
		else
			return false;
	}

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

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

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

	bool operator ==(const String& s)const
	{
		if(strcmp(_str, s._str) == 0)
			return true;
		else
			return false;
	}

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

	void GetExpand(size_t n)
	{
		if(n >= _capacity)//这里是需要多少给你分配多少,也可以一次多分配点空间提高效率
		{
			char* tmp = new char[n+1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}

	void GetStr()
	{
		cout<<_str<<endl;
	}
private:
	char* _str;
	size_t _size;//字符个数
	size_t _capacity;//容量
};
测试用例

int main()
{
	String s1("hello");
	String s2(s1);
	s2.PushBack(" ");
	s2.PushBack("world1");
	s1.GetStr();
	s2.GetStr();

	String s3;
	s3 = s1;
	s3.GetStr();
	s3.PushBack(" world2");
	s3.GetStr();
	s3.PopBack();
	s3.GetStr();

	String s4("hell 3");
	s4.Insert(4, 'o');
	s4.GetStr();
	s4.Insert(6, "world");
	s4.GetStr();
	s4.Erase(0, 6);
	s4.GetStr();

	String s5("hello world4");
	cout<<s5.Find(' ')<<endl;
	cout<<s5.Find('*')<<endl;

	String s6("aabbbcdde");
	cout<<s6.Find("bcdsdjgsdfjgldsjfgldskf")<<endl;
	cout<<s6.Find("BCD")<<endl;
	cout<<s6.operator[](4)<<endl;

	String s7("aabbcdde");
	cout<<(s6!=s7)<<endl;
	cout<<(s6>s7)<<endl;
	cout<<(s6<s7)<<endl;

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值