string模拟实现

发布于:2025-07-13 ⋅ 阅读:(15) ⋅ 点赞:(0)

当我们已经了解了string类的各种函数及其使用,我们可以用已学的C++知识来模拟实现一下string类,实现一下string类中相关的函数

#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
namespace bit
{
	class string
	{
		friend ostream& operator<<(ostream& out, const bit::string& s);
		friend istream& operator>>(istream& in, bit::string& s);
	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(const char* str="")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity+1];
			memcpy(_str, str,_size+1);
		}

		string(const string& s)
		{
			_str = new char[s._capacity +1];
			_size = s._size;
			_capacity = s._capacity;
			memcpy(_str, s._str,_size);
		}
		const char* c_str() const
		{
			return _str;
		}
		size_t size()
		{
			return _size;
		}
		size_t capacity()
		{
			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];
		}
		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n+1];
				strcpy(tmp, _str);
				_capacity = n+1;
				delete[] _str;
				_str = tmp;

			}
		}
		void push_back(const char ch)
		{
			if (_size + 1 > _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity *= 2);
			}
			_str[_size] = ch;
			_size += 1;
			_str[_size] = '\0';
		}
		void append(const char* str)
		{
			if (_size + strlen(str) > _capacity)
			{
				reserve(_size + strlen(str));
			}
			memcpy(_str + _size, str, _size + strlen(str));
			_size += strlen(str);
		}
		string& operator+=(const char ch)
		{
			push_back(ch);
			return *this;
		}
		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}
		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}
		string& operator=(string str)
		{
			swap(str);
			return *this;
		}
		void insert(size_t pos,size_t n,const char ch)
		{
			assert(pos <= _size);
			if (_size + n > _capacity)
			{
				reserve(_size + n);
			}
			size_t tmp = _size;
			while (tmp>=pos&&tmp!=npos)
			{
				_str[tmp + n] = _str[tmp];
				tmp--;
			}
			while (n--)
			{
				_str[pos] = ch;
				pos++;
			}

		}
		void insert(size_t pos, const char* str)
		{
			assert(pos <= _size);
			size_t n = strlen(str);
			if (_size + n > _capacity)
			{
				reserve(_size + n);
			}
			size_t tmp = _size;
			while (tmp >= pos && tmp != npos)
			{
				_str[tmp + n] = _str[tmp];
				tmp--;
			}
			for (size_t i = 0; i < n; i++)
			{
				_str[pos] = str[i];
				pos++;
			}
		}
		void resize(size_t n, char c = '\0')
		{
			if (n > _capacity)
			{
				reserve(n);
				int len = n - _size;
				while (len--)
					_str += c;		
			}
			else
			{
				if (n > _size)
				{
					int len = n - _size;
					while (len--)
						_str += c;
				}
				else
				{
					_size = n;
				}
			}
			

		}
		void erase(size_t pos, size_t n = npos)
		{
			assert(pos <= _size);
			if (n == npos || pos + n >= _size)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				while (n--)
				{
					for (size_t i = pos; i < _size; i++)
					{
						_str[i] = _str[i + 1];
					}
					//pos++;
					_size--;
				}
			}
		}
		string& clear()
		{
			_size = 0;
			_str += '\0';
			return *this;

		}
		size_t find(char ch,size_t pos=0)
		{
			assert(pos < _size);
			for (size_t i = pos; i < _size; i++)
			{
				if (_str[i] == ch)
					return i;
			}
			return npos;
		}
		size_t find(const char* str, size_t pos = 0)
		{
			assert(pos < _size);
			const char* ptr = strstr(_str+pos, str);
			if (ptr)
				return ptr - _str;
			else
				return npos;
		}
		string substr(size_t pos=0, size_t n = npos)
		{
			string tmp;
			if (pos+n >= _size || n == npos)
			{
				n = _size - pos;
			}
			tmp.reserve(n);
			for (size_t i = pos; i < pos+n; i++)
			{
				tmp += _str[i];
			}
			return tmp;
		}
		bool empty(const string& s)const
		{
			if (s._size == 0)
				return true;
			else
				return false;
		}
		bool operator<(const string& s)
		{
			int min = s._size <= _size ? s._size : _size;
			for (int i = 0; i < min; i++)
			{
				if (s[i] > _str[i])
					return true;
				if (s[i] < _str[i])
					return false;
			}
			if (_size < s._size)
				return true;
			else
				return false;
		}

		bool operator<=(const string& s)
		{
			int min = s._size <= _size ? s._size : _size;
			for (int i = 0; i < min; i++)
			{
				if (s[i] > _str[i])
					return true;
				if (s[i] < _str[i])
					return false;
			}
			if (_size <= s._size)
				return true;
			else
				return false;
		}

		bool operator>(const string& s)
		{
			if ((*this<=s))
				return false;
			else
				return true;
		}

		bool operator>=(const string& s)
		{
			if (*this == s)
				return true;
			else if (*this > s)
				return true;
			else
				return false;
		}

		bool operator==(const string& s)
		{
			if (_size != s._size)
				return false;
			else
			{
				if ((*this < s) || (*this > s))
					return false;
				else
					return true;
			}
		}

		bool operator!=(const string& s)
		{
			if (*this == s)
				return false;
			else
				return true;
		}
		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}
	private:
		size_t _size;
		size_t _capacity;
		char* _str;
	public:
		const static size_t npos;
	};
	const size_t string::npos = -1;

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

	istream& operator>>(istream & in, bit::string& s)
	{
		s.clear();
		char ch = in.get();
		while (ch == ' ' || ch == '\n')
		{
			ch = in.get();
		}
		int i = 0;
		char buff[128];

		
		while (ch != '\n')
		{
			
			buff[i] = ch;
			i++;
			if (i == 127)
			{
				s += buff;
				buff[i] = '\0';
				i = 0;
			}
			ch = in.get();
		}
		if (i != 0)
		{
			buff[i] = '\0';
			s += buff;

		}
		return in;
	}
}


网站公告

今日签到

点亮在社区的每一天
去签到