string类的模拟实现

说明:有些函数返回的是String&(引用),有些是String(值),这取决于返回的对象是函数的局部变量还是全局变量。
String.hpp

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

class String
{
public:
	String(const char* str = "")
	{
		if (nullptr == str)
			str = "";

		_size = strlen(str);
		_capacity = _size + 1;
		_str = new char[_size + 1];
		strcpy(_str, str);
	}

	String(const String& s)
		:_size(s._size)
		, _capacity(s._capacity)
	{
		_str = new char[_size + 1];
		strcpy(_str, s._str);
	}

	void Swap(String& s);

	String& operator=(String s);

	~String()
	{
		if (nullptr != _str)
		{
			delete[] _str;
			_str = nullptr;
		}
	}

	void Show();
	const char* c_Str();//获取String类中的C风格字符串
	const char& operator[](size_t index)const;//括号运算符
	
	void Expand(size_t n);//扩容
	void PushBack(char c);//插入一个字符
	void PushBack(const char* str);//插入一个字符串
	void PopBack();//尾部删除一个字符
	void Insert(size_t pos, char c);//pos位置插入一个字符
	void Insert(size_t pos, const char* str);//pos位置插入一个字符串
	void Erase(size_t pos, size_t n = 1);//在pos位置删除n个字符串

	size_t Find(char c);//返回String对象中第一次出现字符c的下标
	size_t Find(const char* str);//返回String对象中第一次出现str的下标

	String operator+(char c);
	String& operator+=(char c);
	String operator+(const char* str);
	String& operator+=(const char* str);

	bool operator>(const String& s);
	bool operator>=(const String& s);
	bool operator<(const String& s);
	bool operator<=(const String& s);
	bool operator==(const String& s);
	bool operator!=(const String& s);

private:
	char* _str;
	size_t _size;
	size_t _capacity;
};

String.cpp

void String::Show()
{
	cout << "_str" << _str << endl;
}

void String::Swap(String& s)
{
	_size = s._size;
	_capacity = s._capacity;
	delete[] _str;
	_str = s._str;
	s._str = new char[1];
}

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

const char* String::c_Str()
{
	return _str;
}

void String::Expand(size_t n)
{
	char* tmp = new char[n];
	memcpy(tmp, _str, _size + 1);
	delete[] _str;
	_str = tmp;
	_capacity = n;
}

void String::PushBack(char c)
{
	Insert(_size, c);
}

void String::PushBack(const char* str)
{
	Insert(_size, str);
}

void String::Insert(size_t pos, char c)
{
	assert(_size > pos);
	if (_size >= _capacity - 1)
	{
		Expand(2 * _capacity + 1);
	}
	for (int i = _size; i > pos; i--)
	{
		_str[i] = _str[i - 1];

	}
	_str[pos] = c;
	++_size;
}

void String::Insert(size_t pos, const char* str)
{
	assert(pos < _size);
	const char* tmp = str;
	size_t size = 0;
	for (; *tmp != '\0'; tmp++)
	{
		++size;
	}
	
	if (_capacity < _size + size)
	{
		Expand(_capacity + size);
	}
	for (int i = _size; i >= pos; i--)
	{
		_str[i + size] = _str[i];
	}
	int i = 0;
	while (*(str + i) != '\0')
	{
		_str[pos + i] = str[i];
		++i;
	}
	_size = _size + size;
}

void String::PopBack()
{
	Erase(_size - 1, 1);
}

void String::Erase(size_t pos, size_t n)
{
	if (0 == _size)
	{
		return;
	}
	if (0 == n)
	{
		return;
	}
	assert(pos <= _size);
	while ((pos + n) <= _capacity)
	{
		_str[pos] = _str[pos + n];
		++pos;
	}
	if (_size - n > 0)
	{
		_size = _size - n;
	}
	else
	{
		_size = 0;
	}
	_str[_size] = 0;
}

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

size_t String::Find(const char* str)
{
	const char* _fast = _str;
	const char* _slow = _str;
	const char* cur = str;
	while (_slow < (_str + _size))
	{
		_fast = _slow;
		if (*_fast == *cur)
		{
			while (*_fast == *cur)
			{
				if (*_fast == '\0')
				{
					break;
				}
				cur++;
				_fast++;
			}
			if (*cur == '\0')
			{
				return _slow - str;
			}
		}
		_slow++;
		cur = str;
	}
	return (size_t)-1;
}

String String::operator+(char c)
{
	String tmp(*this);
	tmp.PushBack(c);
	return tmp;
}

String& String::operator+=(char c)
{
	this->PushBack(c);
	return *this;
}

String String::operator+(const char* str)
{
	String tmp(*this);
	tmp.PushBack(str);
	return tmp;
}

String& String::operator+=(const char* str)
{
	this->PushBack(str);
	return *this;
}

bool String::operator>(const String& s)
{
	const char* _tmp = _str;
	const char* tmp = s._str;
	while (*tmp != '\0' && *_tmp != '\0' && *tmp == *_tmp)
	{
		tmp++;
		_tmp++;
	}
	if (*_tmp > *tmp)
	{
		return true;
	}
	else
	{
		return false;
	}
}

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

bool String::operator<(const String& s)
{
	const char* _tmp = _str;
	const char* tmp = s._str;
	while (*_tmp != '\0' && *tmp != '\0' && *_tmp == *tmp)
	{
		_tmp++;
		tmp++;
	}
	if (*_tmp < *tmp)
	{
		return true;
	}
	return false;
}
 

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

bool String::operator==(const String& s)
{
	const char* _tmp = _str;
	const char* tmp = s._str;
	while (*_tmp == *tmp)
	{
		if (*_tmp == '\0' && *tmp == '\0')
		{
			return true;
		}
		tmp++;
		_tmp++;
	}
	return false;
}

bool String::operator!=(const String& s)
{
	if (!(*this == s))
	{
		return true;
	}
	return false;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值