【C++】STL实现string

#pragma once

#include <iostream>
#include <assert.h>

namespace Test
{
	class string
	{
	public:
		typedef char* iterator;
	public:
		string(const char* str = "")
		{
			if (nullptr == str){
				str = "";
			}
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}

		string(size_t n, char ch)
			: _str(new char[n + 1])
			, _size(n)
			, _capacity(n)
		{
			memset(_str, ch, n);
			_str[_size] = '\0';
		}

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

		template<class Iterator>
		string(Iterator first, Iterator last)
		{
			auto it = first;
			size_t n = 0;
			while (it != last){
				++it, ++n;
			}
			_str = new char[n + 1];
			_size = _capacity = n;
			it = first;
			for (size_t i = 0; i < n; ++i){ 
				_str[i] = *it++;
			}
			_str[n] = '\0';
		}

		string& operator=(const string& s)
		{
			if (this != &s){
				if (_str){
					delete[] _str;
					_str = nullptr;
				}

				size_t size = s.size();
				_str = new char[size];
				strcpy(_str, s._str);
				_capacity = size;
			}
			return *this;
		}

		~string()
		{
			if (_str != nullptr){
				delete[] _str;
				_str = nullptr;
				_size = 0;
				_capacity = 0;
			}
		}
		///
		// 迭代器操作
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}

		///
		// 容量相关
		size_t size()const
		{
			return _size;
		}

		size_t capacity()const
		{
			return _capacity;
		}

		bool empty()const 
		{
			return _size == 0;
		}

		void resize(size_t newsize, char ch = char())
		{
			size_t oldsize = size();
			if (newsize > oldsize){
				if (newsize > capacity()){
					reserve(newsize);
				}
				memset(_str, ch, newsize - oldsize);
			}
			_size = newsize;
			_str[_size] = '\0';
		}

		void reserve(size_t newcapacity)
		{
			size_t oldcapacity = capacity();
			if (newcapacity > oldcapacity){
				char* temp = new char[newcapacity];
				strcpy(temp, _str);
				delete[] _str;
				_str = temp;
				_capacity = newcapacity;
			}
		}

		///
		// 元素访问
		char& operator[](size_t index)
		{
			assert(index < size());
			return _str[index];
		}

		const char& operator[](size_t index)const
		{
			assert(index < size());
			return _str[index];
		}

		//
		// 修改操作:插入需考虑扩容问题
		void push_back(char ch)
		{
			*this += ch;
		}

		string& operator+=(char ch)
		{
			if (size() == capacity()){
				reserve(_capacity * 2);
			}
			_str[_size++] = ch;
			_str[_size] = '\0';
		}

		// 加等字符串
		string& operator+= (const char* str)
		{
			size_t len = strlen(str);
			char* temp = new char[_size + len + 1];
			strcpy(temp, _str);
			strcat(temp, str);
			delete[] _str;
			_str = temp;
			_size += len;
			_capacity = _size;	
			return *this;
		}

		string& operator+=(string& s)
		{
			*this += s._str;
			return *this;
		}

		string& insert(size_t pos, char ch);
		string& erase(size_t pos);

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

		///
		// 特殊操作
		const char* c_str()const
		{
			return _str;
		}

		size_t find(char ch, size_t pos)
		{
			while (pos < _size)
			{
				if (_str[pos] == ch){
					return pos;
				}
				++pos;
			}
			return npos;
		}

		size_t rfind(char ch, size_t pos = npos)
		{
			int cur = (pos == npos) ? _size - 1 : pos;
			while (cur >= 0){
				if (_str[cur] == ch){
					return pos;
				}
				--cur;
			}
			return npos;
		}


	private:
		char* _str;
		size_t _size;
		size_t _capacity;
		static size_t npos;
	};
	static size_t npos = -1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
StringC++ STL(标准模板库)中的一个类,用于表示和操作字符串。String 类提供了许多函数和操作符,使得处理字符串变得更加方便和高效。 要使用 string,需要包含头文件 `<string>`。以下是一些常见的 string 操作及示例: 1. 创建 string: ```cpp std::string myString; // 创建一个空的 string std::string myString = "Hello"; // 创建并初始化一个 string ``` 2. 获取 string 的长度: ```cpp int length = myString.length(); // 获取 string 的长度 ``` 3. 连接两个 string: ```cpp std::string str1 = "Hello"; std::string str2 = "World"; std::string result = str1 + str2; // 连接两个 string ``` 4. 访问和修改 string 的字符: ```cpp char ch = myString[0]; // 获取指定位置的字符 myString[0] = 'H'; // 修改指定位置的字符 ``` 5. 查找子字符串: ```cpp size_t found = myString.find("lo"); // 查找子字符串的位置 if (found != std::string::npos) { // 子字符串存在 } ``` 6. 截取子字符串: ```cpp std::string subString = myString.substr(3, 4); // 截取指定位置和长度的子字符串 ``` 7. 比较两个 string: ```cpp int compareResult = str1.compare(str2); // 比较两个 string 的大小 if (compareResult == 0) { // 两个 string 相等 } ``` 除了上述操作外,string 类还提供了许多其他功能,如插入、删除、替换、转换等。 使用 string 类可以更方便地处理字符串,同时避免了手动管理内存和处理字符数组的复杂性。 这只是 string 的一些基本用法,还有更多的功能和操作可以探索。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值