【STL】手撕 string类

目录


上面我们认识了string类,有了一个大概的理解,下面我们来实现一下 string类的框架,来更好的熟悉 string类,也让我们对其有着更深的理解;

1,string类框架

因为库里本身就有 string类,所以我们命名一个空间,我们在这个空间里面来实现 string类;

我们先写一个 string类的基本框架;

namespace bit
{
	class string
	{
	public:


	private:
		char* _str;
		size_t _capacity;
		size_t _size;
		static size_t npos;
	};
	size_t string::npos = -1;
}

string类 的私有对象由 char类型组成的空间,还有其字符个数,还有其空间的容量,还有一个npos 代表最大值;

2,string(构造)

		//构造
		string(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_size + 1];
			strcpy(_str, str);
		}

我们要初始化 string类,而且必须要让其不为空值,所以我们给一个缺省值 ‘ \0 ‘ ,也就是 ““ ,这样最合适不过了;

3,~string(析构)

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

因为 _str 是 new 出来的空间,所以我们用 delete 来释放空间,然后将 _str 指向空,_size 和 _capacity 归 0 即可;

4,swap(交换)

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

引用 std库里的 swap函数,交换两个string类内的各种数据;

5,string(拷贝构造)

1,常规法

		string(const string& str)
		{
			_str = new char[str._capacity + 1];
			strcpy(_str,str._str);
			_size = str.size();
			_capacity = _size;
		}

这里我们都是用深拷贝的,给 _str 开辟一样大的空间;

然后再逐个字符拷贝,之后再赋予 _size 和 _capacity 对应的值;

2,简便法

		string(const string& str)
		{
			string tmp(str._str);
			swap(tmp);
		}

我们先让 tmp 来替我们完成 str 各个数据的拷贝,然后在与 tmp 进行交换,

6,size (字符长度)

		size_t size()const
		{
			return _size;
		}

这个就直接返回有效字符的长度即可;

7,c_str(返回字符形式的指针)

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

直接返回 _str 即可;

8,iterator(迭代器)

		typedef char* iterator;

		iterator begin()const
		{
			return _str;
		}

		iterator end()const
		{
			return _str + size();
		}

string类里的迭代器的原理其实就是指针,begin()就是首元素地址,end()就是’ \0 ‘ 的地址;

9,operator=(赋值)

1,常规写法

		string& operator=(const string& str)
		{
			if (this != &str)//为了防止两个一样的string类进行赋值
			{
				string tmp(str);
				swap(tmp);
			}
			return *this;
		}

先要判定一下是否一样的 string类进行赋值,然后再赋值拷贝 tmp,在与 tmp 交换,以达到赋值的效果;

2,简便法

		string& operator=(string str)
		{
			swap(str);
			return *this;
		}

这与上面常规写法不同的是,参数不同,这里的参数是拷贝构造的类,可以直接与其交换,不会影响实参的类的数据;

10,operator[](取值)

		char& operator[](size_t pos)
		{
			assert(pos <= _size);
			return _str[pos];
		}

先要进行一下判定,要取值的下标不能大于字符长度,然后直接数组形式返回即可;

11,reserve(空间容量)

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

这种一般是扩容,先判定 n 是否大于原有空间的大小,然后在开辟等大的空间(tmp),在进行拷贝,然后释放掉原空间,再令_str 指向 tmp 再更新 _capacity 的值;

12,push_back(尾插字符)

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

还是先判断空间大小,不够的话则进行扩充当原有空间为 0 时,给其赋值 4 个字节大小,反之二倍,然后再尾插新数据,再更新长度,还有重要的 ‘ \0 ‘ ,这不能漏,漏了的话打印会出问题的,这是容易犯的错误;

13,append(尾插字符串)

		void append(const char* str)
		{
			int pos = strlen(str);
			if (_size + pos > _capacity)
			{
				size_t newcapacity = _size + pos;
				reserve(newcapacity);
			}
			strcpy(_str+_size, str);
			_size += pos;
		}

先计算 str 字符串的大小,然后再进行空间大小判定,不够则扩容然后直接在末尾拷贝即可,然后更新字符长度;

14, operator+=(尾插字符)

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

其实就是套了一层 push_back 的外壳,不过更方便使用些;

15,operator+=(尾插字符串)

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

其实就是套了一层 append 的外壳,不过更方便使用些;

16,insert(插入)

		void insert(size_t pos, char ch)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapacity);
			}
			_size++;
			size_t end = _size;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				end--;
			}
			_str[pos] = ch;
		}

首先还是先判定大小,不够则进行扩容,然后更新字符长度,在从下标 pos 开始,整体向后挪一位,然后再向 pos 位置赋值;

17,insert(插入字符串)

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

和插入字符有着异曲同工之妙,从 pos 位置向后挪要插入字符串的长度,然后再使用 strncpy 进行插入;

18,erase(擦除)

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

先判断 pos 是否越界,如果下标 pos 加上擦除的长度 len 大于字符长度的话,则直接在 pos 位置赋 ‘ \0 ‘ ,然后更新字符长度,如果不大于的话,则直接将后面的字符串拷贝至 pos 位置,以达到覆盖擦除的字符串;

19,find(查找字符)

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

先判断 pos 是否越界,然后就直接遍历查找嘛,找到了就返回其下标,找不到返回 npos ;

20,find(查找字符串)

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

先判断 pos 是否越界,然后用 strstr 从 pos 位置开始找为 stt 的字串,将结果返回给 p;

然后判断 p ,若为空则没有找到则返回 npos,反之则返回 字符串的下标;

21,substr(截取字符串)

		string substr(size_t pos = 0, size_t len = npos)
		{
			assert(pos <= _size);
			size_t end = pos + len;
			if (len == npos || end > _size)
			{
				end = _size;
			}
			string str;
			str.reserve(end-pos);
			for (int i = pos; i < end; i++)
			{
				str += _str[i];
			}
			return str;
		}

截取字符串返回 string类,就是从 pos 位置开始然后尾插 len 个字符,所以我们要找到截取末尾的字符下标 end,如果 pos + len 大于字符串长度 _size ,end 就为 _size;

然后定义一个新string类str,给其开空间,然后再遍历尾插,返回 str 即可;

22,clear(清空)

		void clear()
		{
			_size = 0;
			_str[_size] = '\0';
		}

直接让其变为空字符串,以达到清空的效果;

23,源代码

namespace bit
{
	class string
	{
	public:
		//构造
		string(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_size + 1];
			strcpy(_str, str);
		}

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

		//拷贝构造--传统写法
		//string(const string& str)
		//{
		//	_str = new char[str._capacity + 1];
		//	strcpy(_str,str._str);
		//	_size = str.size();
		//	_capacity = _size;
		//}

		//现代写法
		string(const string& str)
		{
			string tmp(str._str);
			swap(tmp);
		}

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


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

		//指针
		const char* c_str()const
		{
			return _str;
		}

		typedef char* iterator;

		iterator begin()const
		{
			cout <<"char* begin()const"<< endl;
			return _str;
		}

		iterator end()const
		{
			return _str + size();
		}

		//传统写法
		//string& operator=(const string& str)
		//{
		//	if (this != &str)//为了防止两个一样的string类进行赋值
		//	{
		//		string tmp(str);
		//		swap(tmp);
		//	}
		//	return *this;
		//}
		
		//s3=s1
		//现代写法
		string& operator=(string str)
		{
			swap(str);
			return *this;
		}

		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 (_capacity < n)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[]_str;
				_str = tmp;
				_capacity = n;
			}
		}

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

		void append(const char* str)
		{
			int pos = strlen(str);
			if (_size + pos > _capacity)
			{
				size_t newcapacity = _size + pos;
				reserve(newcapacity);
			}
			strcpy(_str+_size, str);
			_size += pos;
		}

		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)
			{
				size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapacity);
			}
			_size++;
			size_t end = _size;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				end--;
			}
			_str[pos] = ch;
		}

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

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

		size_t find(char ch, size_t pos = 0)
		{
			assert(pos <= _size);
			int i = 0;
			for (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* p = strstr(_str + pos, str);
			if (p == nullptr)
			{
				return npos;
			}
			else
			{
				return p - _str;
			}
		}

		string substr(size_t pos = 0, size_t len = npos)
		{
			assert(pos <= _size);
			size_t end = pos + len;
			if (len == npos || end > _size)
			{
				end = _size;
			}
			string str;
			str.reserve(end-pos);
			for (int i = pos; i < end; i++)
			{
				str += _str[i];
			}
			return str;
		}

		void clear()
		{
			_size = 0;
			_str[_size] = '\0';
		}


	private:
		char* _str;
		size_t _capacity;
		size_t _size;
		static size_t npos;
	};
	size_t string::npos = -1;
}

其实string类的实现跟数据结构很类似;

手撕一般string类之后,对string类的理解会更上一层楼的;

以上就是string类常用

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年12月11日
下一篇 2023年12月11日

相关推荐