【C++】stl_string(接口实现)

接口实现:

#pragma once
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
namespace mr
{
	class String
	{
	public:
		typedef char* iterator;
		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		String(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
		//copy (s1)
		String(const String& s)
			:_str(nullptr)
			, _size(0)
			, _capacity(0)
		{
			String tmp(s._str);
			Swap(tmp);
		}

		//copy == s2
		String& operator=(String s)
		{
			Swap(s);
			return *this;
		}

		void Swap(String& s)
		{
			swap(_str, s._str);
			swap(_size, s._size);
			swap(_capacity, s._capacity);
		}

		~String()
		{
			if (_str != nullptr)
			{
				delete[] _str;
				_size = 0;
				_capacity = 0;
			}
		}

		//s1 > s2  hello  world
		bool operator>(const String& s)
		{
			int index = 0;
			while (index != _size || index != s._size)
			{
				if (_size == s._size)
				{
					if (_str[index] == s._str[index])
						++index;
					else if (_str[index] > s._str[index])
						return true;
					else if (_str[index] < s._str[index])
						return false;
					return false;
				}
				else
				{
					if (_str[index] == s._str[index])
						++index;
					else if (_str[index] > s._str[index])
						return true;
					else if (_str[index] < s._str[index])
						return false;
					else if (_size > s._size)
						return true;
					else
						return false;
				}
			}
		}

		bool operator==(const String& s)
		{
			int index = 0;
			if (_size == s._size)
			{
				for (index = 0; index < s._size;)
				{
					if (_str[index] == s._str[index])
						++index;
					else
						return false;
				}
				return true;
			}
			else
				return false;
			
		}


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

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


		void Reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* str = new char[n + 1];
				strcpy(str, _str);

				delete[] _str;
				_str = str;
				_capacity = n;
			}
		}
		void PushBack(char ch)
		{
			if (_size == _capacity)
			{
				Reserve(_capacity * 2+1);
			}
			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}

		void Append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				Reserve(_size + len);
			}
			strcpy(_str + _size, str);
			_size += len;
		}



		String& operator+=(char ch)
		{
			PushBack(ch);
			return *this;
		}


		String& operator+=(const char* str)
		{
			Append(str);
			return *this;
		}

		void Insert(size_t pos, char ch)
		{
			assert(pos > 0 && pos <= _size);
			if (_size == _capacity)
			{
				Reserve(_capacity * 2 + 1);
			}
			size_t end = _size;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				--end;
			}
			_str[pos] = ch;
			++_size;
			_str[_size] = '\0';
			
		}

		void Insert(size_t pos, const char* str)
		{
			assert(pos > 0 && pos < _size);
			if (_size == _capacity)
			{
				Reserve(strlen(str) + _size + 1);
			}
			size_t len = strlen(str);
			size_t end = _size;
			while (end > pos)
			{
				_str[end + len - 1] = _str[end - 1];
				--end;
			}
			while (*str)
			{
				_str[pos++] = *str++;
			}
			_size += len;
			_str[_size] = '\0';
		}

		void Erase(size_t pos, size_t len)
		{
			assert(_size > 0);
			if (pos + len >= _size)
			{
				_size = pos;
				_str[_size] = '\0';
			}
			else
			{
				for (size_t i = pos+len; i < _size; ++i)
				{
					_str[pos] = _str[i];
					++pos;
				}
				_size -= len;
				_str[_size] = '\0';
			}
		}

		size_t Find(char ch, size_t pos = 0)//从前往后找一个字符,返回下标
		{
			for (size_t i = pos; i < _size; ++i)
			{
				if (_str[i] == ch)
					return i;
			}
			return npos;
		}

		//abcdbdbdc
		//bdc
		size_t Find(const char* str, size_t pos = 0)//找一个字符串
		{
			const char* start = strstr(_str+pos, str);
			if (str)
			{
				return start - _str;
			}
			return npos;
		}

		char* c_str()
		{
			return _str;
		}

	private:
		char* _str;
		size_t _size;
		size_t _capacity;
		size_t npos;
	};
	void Test()
	{
		String s1("y");
		String s2("world");
		String copy(s1);
	/*	cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;
		cout << copy.c_str() << endl;
		cout << (s1 > s2) << endl;
		cout << (s1 == s2) << endl;
		cout << (s1 < s2) << endl;
		s1.PushBack('c');
		cout << s1.c_str() << endl;*/
	/*	s1.Append("aaaaaaaa");
		cout << s1.c_str() << endl;*/
		/*s1 += 'p';
		cout << s1.c_str() << endl;*/
		s2.Insert(4, 'b');
		cout << s2.c_str() << endl;


	}
}

主函数:

 int main()
	{
		mr::Test();
		system("pause");
		return 0;
	}

结果截图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值