[手撕STL] string类

前言:

本文将string类中一些主要的函数模拟出来!

1.构造函数

通过缺省值构造函数,可以同时解决默认构造函数

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

2. 拷贝构造和赋值重载

基本写法

这里设计深浅拷贝,如果直接调用默认拷贝构造的话就是浅拷贝,当调用析构函数的时候就会崩溃,原因就是char*指针指向同一块空间了,所以我们必须采取深拷贝的方法,重新开辟一块空间

string(const string& s)
{
	_str=new char[s._capacity+1];
	_size = s._size;
	_capacity = s._capacity;
	strcpy(_str, s._str);
}
string& operator=(const string& s)
{
	if (this != &s)
	{
		char* tmp = new char(s._capacity + 1);
		strcpy(tmp, s._str);
		delete[] _str;
		_size = s._size;
		_capacity = s._capacity;
		_str = tmp;
	}
	return *this;
}

资本思维的写法(1)

通过调用构造函数完成拷贝构造!!

void swap(string& tmp)
{
	std::swap(_str, tmp._str);
	std::swap(_size, tmp._size);
	std::swap(_capacity, tmp._capacity);

}
string(const string& s)
{
	string tmp(s._str);//调用构造函数
	swap(tmp);
}

发挥到极致的资本思维的写法(2)

之前我们都是用的引用传参,现在我们用传值传参,我知道传值传参会生成一个拷贝,所以直接让s代替tmp打工

string& operator=(string s)
{
	swap(s);
	return *this;
}
string& operator=(const string& s)
{
	if (this != &s)
	{
		string tmp(s);//调用拷贝构造
		swap(tmp);
	}
	return *this;
}

3. 析构函数

~string()
{
	_size = 0;
	_capacity = 0;
	_str = nullptr;
	delete[] _str;
}

4. 内置函数实现(实现很常规)

	size_t size() const
	{
		return _size;
	}
	char* c_str()
	{
		return _str;
	}
	size_t capacity() const
	{
		return _capacity;
	}
	const char& operator[](size_t pos) const
	{
		assert(pos < _size);
		return _str[pos];
	}
	char& operator[](size_t pos)
	{
		assert(pos < _size);
		return _str[pos];
	}
	void reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}

	void push_back(char ch)
	{
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		_str[_size] = ch;
		++_size;
		_str[_size] = '\0';
	}
	string& operator+=(char ch)
	{
		 push_back(ch);
		 return *this;
	}
	
	void append(const string& s)
	{
		if (_size + s._size > _capacity)
		{
			reserve(_size + s._size);
		}

		strcpy(_str + _size, s._str);
		_size += s._size;
	}

	string& operator+=(const string& s)
	{
		append(s);
		return *this;
	}

	string& insert(size_t pos, char ch)
	{
		assert(pos <= _size);
		if (_size+1 == _capacity)
		{
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		size_t end = _size + 1;
		while (end > pos)
		{
			_str[end] = _str[end - 1];
			--end;
		}
		_str[pos] = ch;
		++_size;
		return *this;
	}
	string& insert(size_t pos, const string& s)
	{
		assert(pos <= _size);
		if (_size+s._size == _capacity)
		{
			reserve(_size + s._size);
		}
		size_t end = _size + s._size;
		while (end > pos)
		{
			_str[end] = _str[end - s._size];
			--end;
		}
		strncpy(_str + pos, s._str, s._size);
		_size+=s._size;
		return *this;
	}

	void erase(size_t pos, size_t len = npos)
	{
		assert(pos < _size);

		if (len == npos || pos + len >= _size)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			strcpy(_str + pos, _str + pos + len);
			_size -= len;
		}
	}
	void clear()
	{
		_str[0] = '\0';
		_size = 0;
	}


	size_t find(char ch, size_t pos = 0) const
	{
		for (size_t i = pos; i < _size; ++i)
		{
			if (_str[i] == ch)
				return i;
		}
		return npos;
	}
	size_t find(const char* sub, size_t pos = 0) const
	{
		char* n = strstr(_str + pos, sub);
		if (n == nullptr)
			return npos;
		else
			return n - _str;
	}

	string substr(size_t pos, size_t len = npos) const
	{
		assert(pos < _size);
		size_t realLen = len;
		if (len == npos || pos + len > _size)//防止len过大
		{
			realLen = _size - pos;
		}

		string sub;
		for (size_t i = 0; i < realLen; ++i)
		{
			sub += _str[pos + i];
		}

		return sub;
	}
	bool operator>(const string& s) const
	{
		return strcmp(_str, s._str) > 0;
	}
	bool operator==(const string& s) const
	{

		return strcmp(_str, s._str) == 0;

	}
	bool operator>=(const string& s) const
	{
		return strcmp(_str, s._str) >= 0;

	}
	bool operator<=(const string& s) const
	{
		return strcmp(_str, s._str) <= 0;

	}
	bool operator<(const string& s) const
	{
		
		return strcmp(_str, s._str) < 0;
	}
	bool operator!=(const string& s) const
	{
		return !(*this==s);
	}

5. operator>>和operator<<

这里我并没有采用友元函数,直接通[]取到对应的字符
流插入这边做了一个小小的优化,就是我们每次输入都得+=,这用频繁的开辟空间效率较低,我们采用一个临时数组,将我们输入的这些字符都放到临时数组里然后统一+=到字符串中

ostream& operator<<(ostream& out, const string& s)
{
	for (size_t i = 0; i < s.size(); ++i)
	{
		out << s[i];
	}

	return out;
}

istream& operator>>(istream& in, string& s)
{
	s.clear();
	char ch;
	ch = in.get();
	const size_t N = 32;
	char buff[N];
	size_t i = 0;
	while (ch != ' ' && ch != '\n')
	{
		buff[i++] = ch;
		if (i == N - 1)
		{
			buff[i] = '\0';
			s += buff;
			i = 0;
		}
		ch = in.get();
	}
	buff[i] = '\0';
	s += buff;
	return in;
}

完整代码

#pragma once
#include<iostream>
#include<assert.h>
using  std::istream; 
using  std::ostream;

class string
{
public:
	typedef char* iterator;
	typedef const char* const_iterator;
	iterator begin()
	{
		return _str;
	}
	iterator end()
	{
		return _str+_size;
	}
	const_iterator begin() const
	{
		return _str;
	}
	const_iterator end() const
	{
		return _str + _size;
	}
	//string()
	//	:_str(new char[1])
	//	,_size(0)
	//	,_capacity(0)
	//{	
	//	_str[0]='\0';
	//}
	string( const char* str="")
		:_str(new char[strlen(str)+1])
		,_size(strlen(str))
		,_capacity(strlen(str))
	{
		strcpy(_str, str);
	}


	//传统写法
	//string(const string& s)
	//{
	//	_str=new char[s._capacity+1];
	//	_size = s._size;
	//	_capacity = s._capacity;
	//	strcpy(_str, s._str);
	//}

	//string& operator=(const string& s)
	//{
	//	if (this != &s)
	//	{
	//		char* tmp = new char(s._capacity + 1);
	//		strcpy(tmp, s._str);
	//		delete[] _str;
	//		_size = s._size;
	//		_capacity = s._capacity;
	//		_str = tmp;
	//	}
	//	return *this;
	//}


	 //老板思维(1)
	void swap(string& tmp)
	{
		std::swap(_str, tmp._str);
		std::swap(_size, tmp._size);
		std::swap(_capacity, tmp._capacity);

	}
	string(const string& s)
	{
		string tmp(s._str);//调用构造函数
		swap(tmp);
	}
	//string& operator=(const string& s)
	//{
	//	if (this != &s)
	//	{
	//		string tmp(s);
	//		swap(tmp);
	//	}
	//	return *this;
	//}
	//老板思维(2)
	//让s代替tmp打工
	string& operator=(string s)
	{
		swap(s);
		return *this;
	}
	~string()
	{
		_size = 0;
		_capacity = 0;
		_str = nullptr;
		delete[] _str;
	}



	size_t size() const
	{
		return _size;
	}
	char* c_str()
	{
		return _str;
	}
	size_t capacity() const
	{
		return _capacity;
	}
	const char& operator[](size_t pos) const
	{
		assert(pos < _size);
		return _str[pos];
	}
	char& operator[](size_t pos)
	{
		assert(pos < _size);
		return _str[pos];
	}
	void reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}

	void push_back(char ch)
	{
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		_str[_size] = ch;
		++_size;
		_str[_size] = '\0';
	}
	string& operator+=(char ch)
	{
		 push_back(ch);
		 return *this;
	}
	
	void append(const string& s)
	{
		if (_size + s._size > _capacity)
		{
			reserve(_size + s._size);
		}

		strcpy(_str + _size, s._str);
		_size += s._size;
	}

	string& operator+=(const string& s)
	{
		append(s);
		return *this;
	}

	string& insert(size_t pos, char ch)
	{
		assert(pos <= _size);
		if (_size+1 == _capacity)
		{
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		size_t end = _size + 1;
		while (end > pos)
		{
			_str[end] = _str[end - 1];
			--end;
		}
		_str[pos] = ch;
		++_size;
		return *this;
	}
	string& insert(size_t pos, const string& s)
	{
		assert(pos <= _size);
		if (_size+s._size == _capacity)
		{
			reserve(_size + s._size);
		}
		size_t end = _size + s._size;
		while (end > pos)
		{
			_str[end] = _str[end - s._size];
			--end;
		}
		strncpy(_str + pos, s._str, s._size);
		_size+=s._size;
		return *this;
	}

	void erase(size_t pos, size_t len = npos)
	{
		assert(pos < _size);

		if (len == npos || pos + len >= _size)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			strcpy(_str + pos, _str + pos + len);
			_size -= len;
		}
	}
	void clear()
	{
		_str[0] = '\0';
		_size = 0;
	}


	size_t find(char ch, size_t pos = 0) const
	{
		for (size_t i = pos; i < _size; ++i)
		{
			if (_str[i] == ch)
				return i;
		}
		return npos;
	}
	size_t find(const char* sub, size_t pos = 0) const
	{
		char* n = strstr(_str + pos, sub);
		if (n == nullptr)
			return npos;
		else
			return n - _str;
	}

	string substr(size_t pos, size_t len = npos) const
	{
		assert(pos < _size);
		size_t realLen = len;
		if (len == npos || pos + len > _size)//防止len过大
		{
			realLen = _size - pos;
		}

		string sub;
		for (size_t i = 0; i < realLen; ++i)
		{
			sub += _str[pos + i];
		}

		return sub;
	}
	bool operator>(const string& s) const
	{
		return strcmp(_str, s._str) > 0;
	}
	bool operator==(const string& s) const
	{

		return strcmp(_str, s._str) == 0;

	}
	bool operator>=(const string& s) const
	{
		return strcmp(_str, s._str) >= 0;

	}
	bool operator<=(const string& s) const
	{
		return strcmp(_str, s._str) <= 0;

	}
	bool operator<(const string& s) const
	{
		
		return strcmp(_str, s._str) < 0;
	}
	bool operator!=(const string& s) const
	{
		return !(*this==s);
	}
private:
	char* _str;
	int _size;
	int _capacity;


	const static size_t npos = -1;
};
ostream& operator<<(ostream& out, const string& s)
{
	for (size_t i = 0; i < s.size(); ++i)
	{
		out << s[i];
	}

	return out;
}

istream& operator>>(istream& in, string& s)
{
	s.clear();
	char ch;
	ch = in.get();
	const size_t N = 32;
	char buff[N];
	size_t i = 0;
	while (ch != ' ' && ch != '\n')
	{
		buff[i++] = ch;
		if (i == N - 1)
		{
			buff[i] = '\0';
			s += buff;
			i = 0;
		}
		ch = in.get();
	}
	buff[i] = '\0';
	s += buff;
	return in;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

相知-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值