STL-string的模拟实现

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


STL-string函数的模拟实现

#include<string.h>
#include<assert.h>
#include<algorithm>
#include<iostream>
using namespace std;
namespace bit
{
	class string
	{
	public:
		/*string()
			:_str(nullptr)
			, _size(0)
			, _capacity(0)
		{}*///不能让string(const char* str = nullptr)进行合并会报错

		//string(const char* str)
		//	: _str(new char[strlen(str) + 1])//多开一个/0空间
		//	, _size(strlen(str))
		//	, _capacity(strlen(str))//capacity不包含/0
		//{
		//	strcpy(_str, str);
		//}
	/*	string()
			:_str(new char[1])
			, _size(0)
			, _capacity(0)
		{
			_str = '\0';
		}
		string(const char* str)
			:_size(strlen(str))
		{
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}*/
		typedef char* iterator;//迭代器
		typedef const char* const_iterator;//const 迭代器
		const_iterator begin() const
		{
			return _str;
		}

		const_iterator end() const
		{
			return _str + _size;
		}
		
		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str +_size;
		}
		
		size_t size() const
		{
			return _size;
		}

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

		size_t capacity() const
		{
			return _capacity;
		}
		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}

		const char& operator[](size_t pos) const
		{
			assert(pos < _size);

			return _str[pos];
		}
		//string(const char* str = "\0");//这里多开了一个\0
		string(const char* str="")
			:_size(strlen(str))
		{
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);

		}
		const char* c_str() const
		{
			return _str;
		}

		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 : 2 * _capacity);
			}
			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}

		void append(const char* str)//需要插入的字符串
		{//扩容2倍不行,可能装不下
			size_t len = strlen(str);//计算需要插入的字符串长度
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			strcpy(_str+_size, str);//这里把/0覆盖
			_size += len;

		}

		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}

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

		
	void insert(size_t pos, char ch)
		{
			assert(pos <= _size);

			// 扩容2倍
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : 2 * _capacity);
			}

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

			size_t end = _size + 1;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				--end;
			}

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

		void insert(size_t pos,const char* str)
		{
			assert(pos <= _size);
			size_t len = strlen(str);//不能用sizeof,是隐藏的bug
			if (_size + len > _capacity)
			{
				reserve(_size + len+1);
			}

			size_t end = _size + len;
			while (end > pos + len - 1)//要是不减1的话到那个位置就不执行了                            
			{                          //会少算一个
				_str[end] = _str[end - len];
				end--;
			}

			strncpy(_str + pos, str, len);
			_size += len;
		}

		void erase(size_t pos, size_t len = npos)
		{
			assert(pos < _size);//不能删\0;
			//if (len == npos || pos + len >= _size)//溢出
			if (len == npos || len >= _size - pos)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				strcpy(_str + pos, _str + pos + len);
			}
	
		}

		void resize(size_t n,char ch = '\0')
		{
			if (n <= _size)
			{
				_str[n] = '\0';
				_size = n;
			}
			else
			{
				reserve(n);
				for (size_t i = _size; i < n; i++)
				{
					_str[i] = ch;
				}
				_str[n] = '\0';
				_size = n;
			}
		}

		//s2(s1)
		/*string(const string& s)
		{
			_str = new char[s._capacity + 1];
			strcpy(_str, s._str);
			_size = s._size;
			_capacity = s._capacity;
		}*/
		string(const string& s)
		{
			string tmp(s._str);
			swap(tmp);
		}

		//s1=s3;
		string& operator=(const string& s)
		{
			char* tmp = new char[s._capacity + 1];
			strcpy(tmp, s._str);
			delete[] _str;
			_str = tmp;
			_size = s._size;
			_capacity = s._capacity;

			return *this;
		}

		size_t find(const char* sub, size_t pos = 0) const
		{
			assert(pos < _size);

			const char* p = strstr(_str + pos, sub);
			if (p)
			{
				return p - _str;
			}
			else
			{
				return npos;
			}
		}

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

		size_t findchar(char ch,size_t pos = 0) const
		{
			assert(pos < _size);
			for (size_t i = pos; i < _size; i++)
			{
				if (_str[i] == ch)
					return i;
			}

			return npos;
		}

		size_t findstr(const char* sub, size_t pos = 0) const
		{
			assert(pos < _size);

			const char* p = strstr(_str + pos, sub);
			if (p)
			{
				return p - _str;
			}
			else
			{
				return npos;
			}

		}

		string substr(size_t pos, size_t len = 0)
		{
			string sub;
			//if (len == npos || len >= size - pos)
			if (len >= _size - pos)
			{
				for (size_t i = pos; i < _size; i++)
				{
					sub += _str[i];
				}
			}
			else
			{
				for (size_t i = pos; i < pos+len; i++)
				{
					sub += _str[i];
				}
			}
			return sub;
		}


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

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

	public:
		static const int npos;//静态成员变量,属于整个累,属于每个对象
	};

	const int string::npos = -1;

	void swap(string& x, string& y)
	{
		x.swap(y);
	}

	bool operator==(const string& s1,const string& s2)
	{
		int ret = strcmp(s1.c_str(), s2.c_str());
		return ret == 0;
	}

	bool operator<(const string& s1, const string& s2)
	{
		int ret = strcmp(s1.c_str(), s2.c_str());
		return ret < 0;
	}

	bool operator<=(const string& s1, const string& s2)
	{
		return s1 < s2 || s1 == s2;
	}

	bool operator>(const string& s1, const string& s2)
	{
		return !(s1 <= s2);
	}

	bool operator>=(const string& s1, const string& s2)
	{
		return !(s1 < s2);
	}


	bool operator!=(const string& s1, const string& s2)
	{
		return !(s1 == s2);
	}


	ostream& operator<<(ostream& out,const string& s)
	{
		for (auto ch : s)
		{
			out << ch;
		}

		return out;
	}

	//istream& operator<<(istream& in, string& s)
	//{
	//	s.clear();
	//	char ch;
	//	ch = in.get();
	//	while (ch != ' ' && ch != '\n')
	//	{
	//		s += ch;
	//		ch = in.get();
	//	}
	//	return in;
	//}

	istream& operator<<(istream& in, string& s)
	{
		s.clear();
		char ch;
		ch = in.get();
		char buff[128];
		size_t i = 0;
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			//[0,126]
			if (i == 127)
			{
				buff[127] = '\0';
				s += buff;
				i = 0;
			}
			ch = in.get();
		}
		if (i > 0)
		{
			buff[i] = '\0';//超过了,就出去把剩下的加进去
			s += buff;
		}
		return in;
	}

	istream& getline(istream& in, string& s)//获取一行
	{
		s.clear();
		char ch;
		ch = in.get();
		while (ch != '\n')
		{
			s += ch;
			ch = in.get();
		}
		return in;
	}

需要注意的是构造函数的时候 const char* str=“”,而不是=" "(中间多了个空格),这样会产生两个’\0’,还有reserve开空间的时候要额外开出一个’\0’的空间,capacity和size大小是n,那么空间就是n+1

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值