学无先后,达者为师

网站首页 编程语言 正文

string类的模拟实现

作者:DayDay upup 更新时间: 2022-09-22 编程语言
#include<cstring>
#include <iostream>
#include<assert.h>
using namespace std;

namespace zjq
{
	class string
	{
	public:
		string()
			: _size(0)
			,_capacity(0)
		{
			_s = new char[_size + 1];
			strcpy(_s, "");
		}

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

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

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

			delete[] _s;
			_capacity = s._capacity;
			_size = s._size;
			_s = new char[_size +1];
			strcpy(_s, s._s);

			return *this;
		}

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


		char& operator[](size_t pos)
		{

			return _s[pos];
		}

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

		typedef char* iterator;

		typedef const char* const_iterator;


		iterator begin()
		{
			return _s;
		}

		const_iterator begin() const
		{
			return _s;
		}


		iterator end()
		{
			return _s + _size;
		}

		const_iterator end() const
		{
			return _s + _size;
		}



		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n+1];
				strcpy(tmp, _s);
				delete[] _s;
				_s = tmp;
				_capacity = n;
			}
		}


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

		//增
		void push_back(char ch)
		{
			if (_size >= _capacity)
			{
				reserve(2 * _capacity + 1);
			}
			_s[_size] = ch;
			++_size;
			_s[_size] = '\0';
		}

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

		void append(const char* s)
		{
			size_t len = strlen(s);
			if (_capacity <= len + _size)
			{
				reserve(len + _size);
			}
			strcpy(_s + _size, s);
			_size += len;
		}

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

		//插
		string& insert(const char ch, size_t pos = 0)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				reserve(2 * _size);
			}

			size_t end = _size + 1;
			while (end > pos)
			{
				_s[end] = _s[end - 1];
				end--;
			}
			_s[pos] = ch;
			
			return *this;
		}

		string& insert(const char* str, size_t pos = 0)
		{
			size_t len = strlen(str);
			if (len + _size > _capacity)
			{
				reserve(len + _size);
			}
			size_t end = _size + len;
			while (end > pos + len - 1)
			{
				_s[end] = _s[end - len];
				end--;
			}
			strncpy(_s + pos, str, len);
			_size += len;
			return *this;
		}

		string& erase(size_t pos = 0, size_t len = npos)
		{
			if (pos + len >= _size || len == npos)
			{
				_s[pos] = '\0';
			}
			else
			{
				size_t end = pos + len;
				while (end <= _size)
				{
					_s[end - len] = _s[end];
					end++;
				}
			}

			_size -= len;
			return *this;


		}


		//s1<s2
		bool operator<(const string& s)
		{
			return strcmp(_s, s._s) < 0;
		}

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

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

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

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

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

		//查

		size_t find(char ch, size_t pos = 0)
		{
			for (; pos < _size; pos++)
			{
				if (_s[pos] == ch)
				{
					return pos;
				}
			}
			return npos;
		}

		size_t find(const char* str, size_t pos =0)
		{
			char* ptr = strstr(pos + _s, str);
			if (ptr == nullptr)
			{
				return npos;
			}
			else
			{
				return ptr - _s;
			}
		}




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


	private:
		char* _s;
		size_t _size;
		size_t _capacity;
		static size_t npos;
	};
	size_t string::npos = -1;

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


	istream& operator>>(istream& in, string& s)
	{
		char ch;
		ch = in.get();

		while (ch != ' ' && ch != '\n')
		{
			s += ch;
			ch = in.get();
		}

		return in;
	}

}

原文链接:https://blog.csdn.net/zjq_love/article/details/125707284

栏目分类
最近更新