string模拟实现常用类

//头文件

#pragma once
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;

namespace test
{
	class string
	{
	public:

		typedef char* iterator;//在这里迭代器就是指针
		typedef const char* const_iterator;
		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		无参
		//string()
		//	:_str(new char[1])
		//	, _size(0)
		//	, _capacity(0)
		//{
		//	_str[0] = '\0';
		//}

		//带参 缺省
		//string(const char* str = "\0")
		//隐含'\0'
		/*string(const char* str = "")
			:_str(new char[strlen(str) + 1])
			, _size(strlen(str))
			, _capacity(strlen(str))
		{
			//strcpy(_str, str);
			memcpy(_str, str, _size+1);
		}*/
		string(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_size + 1];
			//strcpy(_str, str);
			memcpy(_str, str, _size + 1);
		}

		//s2(s1)
		/*string(const string& s)
			:_str(new char[s._capacity + 1])
			,_size(s._size)
			,_capacity(s._capacity)
		{
			//strcpy(_str, s._str);
			memcpy(_str, str, _size+1);
		}*/
		//s2(s1)另一种写法
		void swap(string& s)
		{
			::swap(_str, s._str);
			::swap(_size, s._size);
			::swap(_capacity, s._capacity);
		}

		string(const string& s)
			:_str(nullptr)
			,_size(0)
			,_capacity(0)
		{
			string tmp(s._str);
			swap(tmp);
		}


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

		//另一种方法1
		string& operator=(const string& s)
		{
			if (this != &s)
			{
				string tmp(s);
				swap(tmp);//this -> tmp
			}
			return *this;
		}
		//另一种方法2
		//string& operator=(string s)
		//{
		//	//s只是拷贝
		//	s._str[0] = '6';
		//	swap(s);
		//	return *this;
		//}

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

		void push_back(char ch)
		{
			if (_size  == _capacity)
			{
				reserve(_capacity == 0 ? 4 : 2 * _capacity);
			}
			_str[_size++] = ch;
			_str[_size] = '\0';
		}

		void append(const char* str)
		{
			size_t len = strlen(str);
			if (len + _size > _capacity)
			{
				reserve(len + _size);
			}
			//strcpy(_str + _size, str);
			memcpy(_str + _size, str, len + 1);
			//strcat(_str, str);没回都需要找\0,效率低
			//strcat(_str + _size, str);如果加_size,效率是差不多的
			_size += len;
		}

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

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

			//发现满了就扩容
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : 2 * _capacity);
			}

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

			_str[pos] = ch;
			++_size;
			return *this;
		}

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

			if (_size + n > _capacity)
			{
				reserve(_size + n);
			}

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

			for (size_t i = pos; i < pos + n; i++)
			{
				_str[i] = ch;
			}

			_size += n;
		}

		string& insert(size_t pos, const char* str)
		{
			//assert(pos <= _size);
			//size_t len = strlen(str);

			扩容
			//if (_size + len > _capacity)
			//{
			//	reserve(_size + len);
			//}

			//size_t end = _size + len;
			//while (end >= pos + len)
			//{
			//	_str[end] = _str[end - len];
			//	--end;
			//}
			//strncpy(_str + pos,str,len);
			//return *this;
			assert(pos <= _size);
			assert(str);
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			size_t end = _size;
			while (end + len >= pos + len)
			{
				_str[end + len] = _str[end];
				--end;
			}
			strncpy(_str + pos, str, len);
			return *this;

		}

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

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

		//返回字符串首地址
		const char* c_str()const
		{
			return _str;
		}

		//返回字符串长度
		size_t size()const
		{
			return _size;
		}

		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];
		}

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

			return npos;
		}
		size_t find(const char* sub, size_t pos = 0)const
		{
			assert(sub);
			assert(pos < _size);
			const char* ptr = strstr(_str + pos,sub);
			if (ptr == nullptr)
			{
				return npos;
			}

			return ptr - _str;
		}

		string substr(size_t pos, size_t len = npos)const 
		{
			assert(pos < _size);
			size_t realLen = len;
			if (len == npos || len + pos > _size)
			{
				realLen = _size - pos;
			}

			string sub;
			for (size_t i = pos; i < pos + realLen; i++)
			{
				sub += _str[i];
			}
			sub += '\0';
			return sub;
		}

		void resize(size_t n, char ch = '\0')
		{
			if (n > _size)
			{
				//插入数据
				reserve(n);
				for (size_t i = _size; i < n; i++)
				{
					_str[i] = ch;
				}
				_str[n] = '\0';
				_size = n;
			}
			else
			{
				//删除数据
				_str[n] = '\0';
				_size = n;
			}
		}

		//在这里比较大小时,会出现一下情况
		//hello
		//hello\0pppp
		//这时strcmp并不能满足我们的需求,因为他遇到\0就结束了
		bool operator>(const string& s) const
		{
			//return strcmp(_str, s._str) > 0;


			/*size_t i1 = 0;
			size_t i2 = 0;
			while (i1 < _size && i2 < s._size)
			{
				if (_str[i1] < s._str[i2])
				{
					return true;
				}
				else
				{
					if (_str[i1] > s._str[i2])
					{
						return false;
					}
				}
			}

			if (i1 == _size && i2 != s._size)
			{
				return true;
			}
			else
			{
				return true;
			}*/
			//return i1 == _size && i2 != s._size;
			//return _size < s._size;

			int ret = memcmp(_str, s._str, _size < s._size ? _size : s._size);
			return ret == 0 ? _size < s._size : ret < 0;
		}
		bool operator==(const string& s)const
		{
			//return strcmp(_str, s._str) == 0;
			return memcpy(_str, s._str, _size) == 0 && _size == s._size;
		}
		bool operator>=(const string& s)
		{
			return *this == s || *this > s;
			//return _str == s._str || _str > s._str;
		}
		bool operator<=(const string& s)const
		{
			return *this == s || !(*this > s);
			//return _str == s._str || !(_str > s._str);
		}
		bool operator!=(const string& s)
		{
			//return !(_str == s._str);
			return !(*this == s);
		}
	
	private:
		char* _str;
		size_t _size;
		size_t _capacity;


	public:
		//static size_t npos;//静态成员类外定义
		
		//const static 可以直接定义,直接可以当成初始化,语法特殊处理
		const static size_t npos = -1;

	};
	//size_t string::npos = -1;

	ostream& operator<<(ostream& out, const string& s)
	{
		for (size_t i = 0; i < s.size(); ++i)
		{
			out << s[i];
		}
		return out;
	}

	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		char ch;
		ch = in.get();
		const size_t N = 32;
		char buff[N];
		size_t i = 0;
		//in >> ch;//默认不会读空格和换行
		while(ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == N-1)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			ch = in.get();
			//in >> ch;
		}
		buff[i] = '\0';
		s += buff;
		return in;
	}


	//测试
	void test_string()
	{
		string s1("hello world");
		string s2;

		//返回首地址输出
		cout << "返回首地址输出" << endl;
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;


		//方括号输出
		cout << "方括号输出" << endl;
		for (size_t i = 0; i < s1.size(); i++)
		{
			cout << s1[i] << " ";
		}
		cout << endl;

		for (size_t i = 0; i < s1.size(); i++)
		{
			s1[i]++;
			cout << s1[i] << " ";
		}
		cout << endl;

		//迭代器输出
		cout << "迭代器输出" << endl;
		string::iterator it = s1.begin();
		while (it != s1.end())
		{
			(*it)++;
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	//测试2
	void test_string2()
	{
		string s1("hello world");
		string s2(s1);
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;

		s2[0] = 'x';
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;

		s2 = s1;
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;
	}

	//测试3
	void test_string3()
	{
		string s1("hello world");
		string s3;

		s3 = s1;
		s3 = s3;
		cout << s1.c_str() << endl;
		cout << s3.c_str() << endl;
		cout << s3.capacity() << endl;

		s3 += 'x';
		s3 += 'x';
		s3 += 'x';
		s3 += 'x';
		s3 += 'x';
		s3 += 'x';
		s3 += 'x';
		s3 += "666666";
		cout << s3.c_str() << endl;
		cout << s3.capacity() << endl;
	}

	//测试4
	void test_string4()
	{
		string s1("hello ");
		
		s1.push_back('w');
		cout << s1.c_str() << endl;

		s1.append("orld");
		cout << s1.c_str() << endl;

		s1.insert(0,'x');
		cout << s1.c_str() << endl;

		s1.insert(0, "333333 ");
		cout << s1.c_str() << endl;

		s1.insert(3, 6,'6');
		cout << s1.c_str() << endl;
	}

	//测试5
	void test_string5()
	{
		string s1("hello ");
		s1.erase(1);
		cout << s1.c_str() << endl;

		string s2("hello ");
		s1.erase(1,10);
		cout << s1.c_str() << endl;

		string s3("hello ");
		s1.erase(1, 2);
		cout << s1.c_str() << endl;
	}

	//测试6
	void test_string6()
	{
		string s1("hello ");
		cout << s1<< endl;
		cout << s1.c_str() << endl;

		s1 += '\0';
		s1 += "world";
		cout << s1 << endl;
		cout << s1.c_str() << endl;

		string s2("hello"), s3;
		cin >> s2 >> s3;
		cout << s2 <<"----------" << s3 << endl;
	}
	//测试7
	void test_string7()
	{
		string s1("hello ");
		s1.resize(7, '7');
		cout << s1 << endl;

		string s2("hello ");
		s2.resize(1);
		cout << s2 << endl;
	}

	//测试8
	void test_string8()
	{
		string s1("aello ");
		string s2("bello ");
		bool P = (s1 <= s2);
		cout << P << endl;
	}
}



//源文件
#define _CRT_SECURE_NO_WARNINGS 1


//void ToString_test()
//{
//	int ival;
//	double dval;
//	cin >> ival >> dval;
//	cout << "整型数据为:" << ival << "  双精度浮点型数据为:" << dval << endl;
//	string istr = to_string(ival);
//	string dstr = to_string(dval);
//	cout << istr << endl;
//	cout << dstr << endl;
//
//	istr = "-101";
//	dstr = "999.99";
//
//	ival = stoi(istr, nullptr, 2);
//	dval = stod(dstr);
//
//	cout << ival << endl;
//	cout << dval << endl;
//}
//
//int main()
//{
//	ToString_test();
//	return 0;
//}


#include "string.h"

// int main()
//{
//	test::string url("http://www.cplusplus.com/reference/string/string/find/");
//	cout << url << endl;
//	size_t start = url.find("://");
//	if (start == string::npos)
//	{
//		cout << "invalid url" << endl;
//	}
//
//	start += 3;
//	size_t finish = url.find('/', start);
//	test::string address = url.substr(start, finish - start);
//	cout << address << endl;
//
//
//	return 0;
//}

int main()
{
	try
	{
		test::test_string8();
	}
	catch (const exception& e)
	{
		cout << e.what() << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值