C++string类的模拟实现

string类的模拟实现

在这里插入图片描述

深浅拷贝
在这里插入图片描述
浅拷贝/值拷贝
编译器默认生成的拷贝构造完成浅拷贝
s1和s2的str指向同一块空间,会析构两次

在这里插入图片描述
swap

g++
引用计数解决析构多次
在这里插入图片描述
写时拷贝解决“一个修改会影响另一个”
在这里插入图片描述
在这里插入图片描述

test.cpp

#include"string.h"

int main()
{
	//bit::test_string8();
	std::string s1("11111");
	cout << sizeof(s1) << endl;
	//vs2022 32位28  64位40
	//g++    4/8  只有一个指针(结构指针)   g++默认64位
	return 0;
}

strin.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
namespace bit
{
	class string
	{
	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()
		//	:_str(new char[1])
		//	,_size(0)
		//	,_capacity(0)
		//{
		//	_str[0] = '\0';
		//}
		strlen调用了三次
		//string(const char* str)
		//	:_str(new char[strlen(str)+1])
		//	,_size(strlen(str))
		//	,_capacity(strlen(str))
		//{
		//	strcpy(_str, str);
		//}

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

		//string(const char* str = "\0")
		string(const char* str = "")
			:_size(strlen(str))
		{
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
		// s2(s1)    传统写法
		/*string(const string& s)
		{
			_str = new char[s._capacity + 1];
			strcpy(_str, s._str);
			_size = s._size;
			_capacity = s._capacity;
		}*/
		// 现代写法
		string(const string& s)
		{
			string tmp(s._str);
			swap(tmp);
		}
		// s2 = s1
		/*string& operator = (const string & s)
		{
			char* tmp = new char[s._capacity + 1];
			strcpy(tmp,s._str);

			delete[] _str;
			_str = tmp;
			_size = s._size;
			_capacity = s._capacity;
			return *this;
		}*/

		/*string& operator = (const string& s)
		{
			string ss(s);
			swap(ss);
			return *this;
		}*/
		//现代写法
		string& operator = (string tmp)//传值传参调用拷贝构造	
		{
			swap(tmp);
			return *this;
		}
		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0; 
		}

		// 遍历
		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//用重载解决const对象问题
		{
			assert(pos < _size);
			return _str[pos];
		}

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

				_capacity = n;
			}
		}
		void push_back(char ch)
		{
			//扩容二倍
			/*if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : 2 * _capacity);
			}

			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';*/
			insert(_size, ch);
		}

		void append(const char* str)
		{
			//扩容
			//size_t len = strlen(str);
			//if (_size + len > _capacity)
			//{
			//	reserve(_size + len);
			//}

			//strcpy(_str + _size, str);//strcpy会拷贝'\0'
			//_size += len;
			insert(_size, str);
		}

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

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

		void insert(size_t pos, char ch)
		{
			assert(pos <= _size);
			//扩容二倍
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : 2 * _capacity);
			}
			/*int end = _size;//int整形提升成size_t 
			//size_t end =_size;
			while (end >= (int)pos)
			{
				_str[end + 1] = _str[end];
				--end;
			}*/
			//另一种解决方法
			size_t end =_size + 1;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				--end;
			}
			_str[pos] = ch;
			++_size;
		}

		void insert(size_t pos, const char* str)
		{
			assert(pos <= _size);
			//扩容
			size_t len = strlen(str);//不能用sizeof
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}

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

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

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

		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}

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

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

		string substr(size_t pos = 0,size_t len =npos)
		{
			string sub;
			if (len > _size - pos)
			{
				for (size_t i = pos; i < _size; i++)
				{
					sub += _str[i];
				}
			}
			else
			{
				for (size_t i = pos; i < pos + len; i++)
				{
					sub += _str[i];
				}
			}
			return sub;
		}

		void clear()
		{
			_size = 0;
			_str[_size];
		}
	private:
		char* _str = nullptr;
		size_t _size = 0;
		size_t _capacity = 0;
	public:
		static const int npos;
	};
	const int string::npos = -1;

	void swap(string& s1, string& s2)
	{
		s1.swap(s2);
	}

	bool operator==(const string& s1,const string& s2)
	{
		int ret = strcmp(s1.c_str(), s2.c_str());
		return ret == 0;
	}

	bool operator<(const string & s1, const string & s2)
	{
		int ret = strcmp(s1.c_str(), s2.c_str());
		return ret < 0;
	}

	bool operator<=(const string& s1, const string& s2)
	{
		return s1 < s2 || s1 == s2;
	}

	bool operator>(const string& s1, const string& s2)
	{
		return !(s1 <= s2);
	}

	bool operator>=(const string& s1, const string& s2)
	{
		return !(s1 < s2);
	}

	bool operator!=(const string& s1, const string& s2)
	{
		return !(s1 == s2);
	}

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

	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		char ch;
		//in >> ch;
		ch = in.get();
		char buff[128];
		size_t i = 0;
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == 127)
			{
				buff[127] = '\0';
				s += buff;
				i = 0;
			}
			ch = in.get();
		}
		if (i > 0)
		{
			buff[i] = '\0';
			s += buff;
		}

		return in;
	}

	//istream& getline(istream& in, string& s)
	//{
	//	s.clear();
	//	char ch;
	//	//in >> ch;
	//	ch = in.get();
	//	while (ch != '\n')
	//	{
	//		s += ch;
	//		ch = in.get();
	//	}
	//	return in;
	//}

	istream& getline(istream& in, string& s)
	{
		s.clear();
		char ch;
		//in >> ch;
		ch = in.get();
		char buff[128];
		size_t i = 0;
		while (ch != '\n')
		{
			buff[i++] = ch;
			if (i == 127)
			{
				buff[127] = '\0';
				s += buff;
				i = 0;
			}
			ch = in.get();
		}
		if (i > 0)
		{
			buff[i] = '\0';
			s += buff;
		}

		return in;
	}
	void test_string1()
	{
		string s1("hello world");
		string s2;
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;

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

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

		const string s3("xxxxx");
		for (size_t i = 0; i < s3.size(); i++)
		{
			//s3[i]++;
			cout << s3[i] << " ";
		}
		cout << endl;

		//C语言越界检查是一种抽查
		//int a[10];
		//a[10];
		//a[11];//越界读检查不出来
		//a[10] = 1;//越界写是一种抽查
		//a[15] = 1;
	}

	void test_string2()
	{
		string s1("hello world");
		string::iterator it1 = s1.begin();
		while (it1 != s1.end())
		{
			*it1 -= 1;
			cout << *it1 << " ";
			++it1;
		}
		cout << endl;

		for (auto ch : s1)//改成Begin()就不行了范围for是按固定名字找的,按规则替换
		{
			cout << ch << " ";
		}
		cout << endl;

		const string s2("hello world");
		string::const_iterator it2 = s2.begin();
		while (it2 != s2.end())
		{
			cout << *it2 << " ";
			++it2;
		}
		cout << endl;
		for (auto ch : s2)		{
			cout << ch << " ";
		}
		cout << endl;
	}

	void test_string3()
	{
		string s1("hello world");
		s1.push_back('1');
		s1.push_back('2');
		cout << s1.c_str() << endl;
		
		s1 += 'x';
		s1 += "yyyy";
		cout << s1.c_str() << endl;

		string s2("hello world");
		s2.insert(1, "aaa");
		cout << s2.c_str() << endl;
	}

	void test_string4()
	{
		string s1("hello world");
		cout << s1.c_str() << endl;
		s1.erase(6, 3);
		cout << s1.c_str() << endl;

		s1.erase(6, 30);
		cout << s1.c_str() << endl;
		s1.erase(0);
		cout << s1.c_str() << endl;

		
		string s2("hello world");
		cout << s2.c_str() << endl;
		s2.resize(5);
		cout << s2.c_str() << endl;
		s2.resize(20,'x');
		cout << s2.c_str() << endl;
	}

	void test_string5()
	{
		string s1("hello world");
		cout << s1.c_str() << endl;
		string s2(s1);
		cout << s2.c_str() << endl;

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

		string s3("xxxxx");
		s1 = s3;
		cout << s1.c_str() << endl;
		cout << s3.c_str() << endl;
	}

	void test_string6()
	{
		string s1("hello world");
		cout << s1.c_str() << endl;

		s1.insert(4, "xxxx");
		cout << s1.c_str() << endl;

		string s2("xxxxxxx");
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;
		swap(s1, s2);
		//s2.swap(s1);
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;
	}

	void test_string7()
	{
		string url("https://legacy.cplusplus.com/reference/string/string/substr/");
		string protocol, domain, uri;
		size_t i1 = url.find(':');
		if (i1 != string::npos)
		{
			protocol = url.substr(0, i1 - 0);
			cout << protocol.c_str() << endl;
		}
		// strchar
		size_t i2 = url.find('/', i1 + 3);
		if (i2 != string::npos)
		{
			domain = url.substr(i1 + 3, i2 - i1 - 3);
			cout << domain.c_str() << endl;
			uri = url.substr(i2 + 1);
			cout << uri.c_str() << endl;
		}
	}

	void test_string8()
	{
		string s1("hello world");
		string s2("hello world");
		cout << (s1 == s2) << endl;

		cout << ("hello world" == s1) << endl;
		cout << (s1 == "hello world") << endl;

		cout << s1 << endl;

		cin >> s1 >>s2;
		cout << s1 << endl;
		cout << s2 << endl;


		getline(cin, s1);
		cout << s1 << endl;
	}

	void test_string9()
	{
		string s1("hello world");
		string s2(s1);
		cout << s1 << endl;
		cout << s2 << endl;

		string s3("xxxxxxx");
		s1 = s3;
	}
}

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值