string模拟实现

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

cpp 复制代码
#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;
	}
}
相关推荐
YIN_尹1 小时前
【Linux系统编程】进程地址空间
linux·c++
EverestVIP1 小时前
C++中空类通常大小为1的原理
c++
网域小星球2 小时前
C++ 从 0 入门(六)|C++ 面试必知:运算符重载、异常处理、动态内存进阶(终极补充)
开发语言·c++·面试
晚会者荣2 小时前
红黑树的插入(有图)
c++
John.Lewis2 小时前
C++进阶(12)附加学习:STL之空间配置器(了解)
开发语言·c++·笔记
汉克老师2 小时前
GESP2023年12月认证C++三级( 第三部分编程题(2、单位转换))
c++·string·单位转换·gesp三级·gesp3级
cpp_25013 小时前
P2347 [NOIP 1996 提高组] 砝码称重
数据结构·c++·算法·题解·洛谷·noip·背包dp
Hugh-Yu-1301234 小时前
二元一次方程组求解器c++代码
开发语言·c++·算法
楼田莉子4 小时前
同步/异步日志系统:日志落地模块\日志器模块\异步日志模块
linux·服务器·c++·学习·设计模式
文祐4 小时前
C++类之虚函数表及其内存布局
开发语言·c++