string的模拟实现 (6)

news2025/5/22 5:37:17

目录

1.string.h

2.string.cpp

3.test.cpp

4.一些注意点


本篇博客就学习下如何模拟实现简易版的string类,学好string类后面学习其他容器也会更轻松些。

代码实现如下:

1.string.h

#define  _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <iostream>
#include <assert.h>
#include <string>
using namespace std;
namespace lwx {
	class string {
	private:
		char* _str=nullptr;
		size_t _size=0;
		size_t _capacity=0;
	public:
		using iterator = char*;
		using const_iterator = const char*;
		string(const char* s = "");
		string(const string& str);
		string& operator=(const string& str);
		~string();

		size_t size()const {
			return _size;
		}

		size_t capacity()const {
			return _capacity;
		}

		char* c_str()const {
			return _str;
		}

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

		string substr(size_t pos, size_t len = npos)const;

		iterator begin() {
			return _str;
		}
		iterator end() {
			return _str + _size;
		}

		const_iterator begin() const {
			return _str;
		}
		const_iterator end()const {
			return _str + _size;
		}

		string& operator+=(char c);
		string& operator+=(const char* s);

		void push_back(char c);
		void append(const char* s);

		void erase(size_t pos, size_t len = npos);

		void insert(size_t pos, char c);
		void insert(size_t pos, const char* s);

		size_t find(char c, size_t pos=0)const;
		size_t find(const char* s, size_t pos=0)const;

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

		char& operator[](size_t n) {
			assert(n <= _size);
			return _str[n];
		}
		const char& operator[](size_t n)const {
			assert(n <= _size);
			return _str[n];
		}
	public:
		static const size_t npos;
	};
	bool operator==(const string& st, const string& str);
	bool operator!=(const string& st, const string& str);
	bool operator>=(const string& st, const string& str);
	bool operator<=(const string& st, const string& str);
	bool operator>(const string& st, const string& str);
	bool operator<(const string& st, const string& str);

    ostream& operator<<(ostream& os, const string& str);
	istream& operator>>(istream& is, string& str);
}

2.string.cpp

#define  _CRT_SECURE_NO_WARNINGS 1
#include"string.h"
namespace lwx {
	const size_t string::npos = -1;
	string::string(const char* s)
		:_size(strlen(s))
	{
		_capacity = _size;
		_str = new char[_capacity + 1];
		strcpy(_str, s);
	}
	string::string(const string& str) {
		_size = str._size;
		_capacity = str._capacity;
		_str = new char[_capacity + 1];
		strcpy(_str, str._str);
	}
	string& string::operator=(const string& str) {
		if (*this != str) {
			delete[] _str;
			_size = str._size;
			_capacity = str._capacity;
			_str = new char[_capacity+1];
			strcpy(_str, str._str);
		}
		return *this;
	}
	string::~string() {
		delete[] _str;
		_str = nullptr;
		_size = 0;
		_capacity = 0;
	}

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

	}
	void string::append(const char* s) {
		if (_size + strlen(s) > _capacity) {
			reserve(2 * _capacity);
		}
		if (_size + strlen(s) > _capacity)
			reserve(_size + strlen(s));
		strcpy(_str+_size, s);
		
		_size += strlen(s);
	}
	string& string::operator+=(char c) {
		push_back(c);
		return *this;
	}
	string& string::operator+=(const char* s) {
		append(s);
		return *this;
	}

	string string::substr(size_t pos,size_t len) const{
		assert(pos < _size);
		if (len>_size-pos) {
			len = _size - pos;
		}
		lwx::string sub;
		sub.reserve(len+1);
		for (size_t i = 0; i < len; i++) {
			sub._str[i] = _str[pos + i];
		}
		return sub;
	}
	void string::erase(size_t pos, size_t len ) {
		assert(pos <= _size);
		if (len >= _size - pos) {
			_str[pos] = '\0';
			_size = pos;
		}
		else {
			for (size_t i = 0; i <len; i++) {
				_str[pos+i] = _str[pos + len+i];
			}
		}
		_size -= len;
	}

	void string::insert(size_t pos, char c) {
		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] = c;
		_size++;
	}
	void string::insert(size_t pos, const char* s) {
		assert(pos <= _size);
		size_t n = strlen(s);
		if (_size + n > _capacity)
			reserve(2 * _capacity);
		if (_size + n > _capacity)
			reserve(_size + n);
		size_t end = _size + n;
		while (n > 0) {
			_str[end + n] = _str[end];
			n--;
		}
		for (size_t i = 0; i < n; i++) {
			_str[pos + i] = s[i];
		}
		_size += n;
	}
	size_t string::find(char c, size_t pos) const{
		assert(pos < _size);
		for (size_t i = pos; i < _size; i++) {
			if (_str[i] == c)
				return i;
		}
		return npos;
	}
	size_t string::find(const char* s, size_t pos) const{
		assert(pos < _size);
		const char* p = strstr(_str + pos, s);
		if (p == nullptr) {
			return npos;
		}
		else {
			return p - _str;
		}
		return npos;
	}
	bool operator==(const string& st, const string& str) {
		return strcmp(st.c_str(), str.c_str()) == 0;
	}
	bool operator!=(const string& st, const string& str) {
		return !(st == str);
	}
	bool operator>=(const string& st, const string& str) {
		return !(st < str);
	}
	bool operator<=(const string& st, const string& str) {
		return !(st > str);
	}
	bool operator>(const string& st, const string& str) {
		return strcmp(st.c_str(), str.c_str()) > 0;
	}
	bool operator<(const string& st, const string& str) {
		return !(st > str || st == str);
	}

	ostream& operator<<(ostream& os,const string& str) {
		for (size_t i = 0; i < str.size(); i++) {
			os << str[i];
		}
		return os;
	}
	istream& operator>>(istream& is, string& str) {
		str.clear();
		char c;
		c = is.get();
		while (c != ' ' && c != '\n') {
			str += c;
			c = is.get();
		}
		return is;
	}
}

3.test.cpp

#define  _CRT_SECURE_NO_WARNINGS 1
#include"string.h"

void test_string1()
{
	lwx::string s2;
	cout << s2.c_str() << endl;

	lwx::string s1("hello world");
	cout << s1.c_str() << endl;

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

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

	// 迭代器 -- 像指针一样的对象
	lwx::string::iterator it1 = s1.begin();
	while (it1 != s1.end())
	{
		(*it1)--;

		++it1;
	}
	cout << endl;

	it1 = s1.begin();
	while (it1 != s1.end())
	{
		cout << *it1 << " ";
		++it1;
	}
	cout << endl;

	// 修改
	// 底层是迭代器的支持
	// 意味着支持迭代器就支持范围for
	for (auto& ch : s1)
	{
		ch++;
	}

	for (auto ch : s1)
	{
		cout << ch << " ";
	}
	cout << endl;

	const lwx::string s3("xxxxxxxxx");
	for (auto& ch : s3)
	{
		//ch++;
		cout << ch << " ";
	}
	cout << endl;
}

void test_string2()
{
	lwx::string s1("hello world");
	cout << s1.c_str() << endl;

	s1 += '#';
	s1 += "#hello world";
	cout << s1.c_str() << endl;

	lwx::string s2("hello world");
	cout << s2.c_str() << endl;
	s2.insert(6, 'x');
	cout << s2.c_str() << endl;

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

	lwx::string s3("hello world");
	cout << s3.c_str() << endl;
	s3.insert(6, "xxx");
	cout << s3.c_str() << endl;

	s3.insert(0, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
	cout << s3.c_str() << endl;
}

void test_string3()
{
	lwx::string s1("hello world");
	cout << s1.c_str() << endl;

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

	s1.erase(5, 20);
	cout << s1.c_str() << endl;

	s1.erase(3);
	cout << s1.c_str() << endl;
}

void test_string4()
{
	lwx::string s1("hello world");
	cout << s1.find(' ') << endl;
	cout << s1.find("wo") << endl;

	lwx::string s2 = "https://legacy.cplusplus.com/reference/cstring/strstr/?kw=strstr";
	//bit::string s2 = "https://blog.csdn.net/ww753951/article/details/130427526";
	size_t pos1 = s2.find(':');
	size_t pos2 = s2.find('/', pos1 + 3);
	if (pos1 != string::npos && pos2 != string::npos)
	{
		lwx::string domain = s2.substr(pos1 + 3, pos2 - (pos1 + 3));
		cout << domain.c_str() << endl;

		lwx::string uri = s2.substr(pos2 + 1);
		cout << uri.c_str() << endl;
	}
}

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

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

	lwx::string s3("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
	s1 = s3;
	cout << s1.c_str() << endl;
	cout << s3.c_str() << endl;

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

void test_string6()
{
	lwx::string s1("hello world");
	lwx::string s2(s1);
	lwx::string s3 = s1;
	// 构造+拷贝 ->优化直接构造
	lwx::string s4 = "hello world";

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

	//operator<<(cout, s1); 
	cout << s1 << endl;

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

	std::string ss1("hello world");
	cin >> ss1;
	cout << ss1 << endl;
}

int main()
{
	test_string6();

	return 0;
}

4.一些注意点

①我们定义的string类会和库里面有冲突的风险,所以我们可以用命名空间namespace进行封装。

②编写默认构造函数时,我们不能给_str(nullptr)缺省值,因为cout<<(const char*)_str<<endl不会当指针打印,它会自动识别类型,觉得你是想打印字符串,而打印字符串遇到'\0'才会终止,但_str为空指针,这就有解引用空指针问题了。

但标准库里的string不会有这种问题,解决方法:直接给'\0'开一个空间就行了

③在string这里申请空间要多给一个空间用来存放'\0',,但capacity不将'\0'计算其中,空间真实大小=_capacity+1。

④在前面类和对象下我们说到,尽可能的使用初始化列表,但在这里使用比较别捏,三个strlen(),strlen是在运行时计算的,3个O(n),还是很坑的。

那我们改一下,下面这个比上面的运行效率时好多了,但是这种写法是错的。因为我们之前说过,初始化列表会按声明的顺序初始化,先走_str,再走_size,所以我们还得把声明顺序变了才行,但声明顺序变了又不顺我们的习惯(先声明指针),而且过后还要把数据拷贝出来,所以说我们是得尽可能使用初始化列表,但有些东西是初始化列表搞不定的,改该用函数体还是得用,不能说有了初始化列表就不用函数体了。

最终我们可以改成,这样就不用管顺序了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2336927.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【野火模型】利用深度神经网络替代 ELMv1 野火参数化:机制、实现与性能评估

目录 一、ELMv1 野火过程表示法&#xff08;BASE-Fire&#xff09;关键机制野火模拟的核心过程 二、采用神经网络模拟野火过程三、总结参考 一、ELMv1 野火过程表示法&#xff08;BASE-Fire&#xff09; ELMv1 中的野火模型&#xff08;称为 BASE-Fire&#xff09;源自 Commun…

红宝书第四十七讲:Node.js服务器框架解析:Express vs Koa 完全指南

红宝书第四十七讲&#xff1a;Node.js服务器框架解析&#xff1a;Express vs Koa 完全指南 资料取自《JavaScript高级程序设计&#xff08;第5版&#xff09;》。 查看总目录&#xff1a;红宝书学习大纲 一、框架定位&#xff1a;HTTP服务器的工具箱 共同功能&#xff1a; 快…

嵌入式Linux设备使用Go语言快速构建Web服务,实现设备参数配置管理方案探究

本文探讨&#xff0c;利用Go语言及gin框架在嵌入式Linux设备上高效搭建Web服务器&#xff0c;以实现设备参数的网页配置。通过gin框架&#xff0c;我们可以在几分钟内创建一个功能完善的管理界面&#xff0c;方便对诸如集中器&#xff0c;集线器等没有界面的嵌入式设备的管理。…

【NLP 59、大模型应用 —— 字节对编码 bpe 算法】

目录 一、词表的构造问题 二、bpe(byte pair encoding) 压缩算法 算法步骤 示例&#xff1a; 步骤 1&#xff1a;初始化符号表和频率统计 步骤 2&#xff1a;统计相邻符号对的频率 步骤 3&#xff1a;合并最高频的符号对 步骤 4&#xff1a;重复合并直至终止条件 三、bpe在NLP中…

Python对ppt进行文本替换、插入图片、生成表格

目录 1. 安装pptx2. 文本替换和插入图片3. 生成表格 1. 安装pptx pip install python-pptx2. 文本替换和插入图片 文本通过占位符例如{{$xxx}}进行标记&#xff0c;然后进行替换&#xff1b;图片通过ppt中的图形和图片中的占位符进行标记ppt如下 具体实现 from pptx import …

AI(学习笔记第一课) 在vscode中配置continue

文章目录 AI(学习笔记第一课) 在vscode中配置continue学习内容&#xff1a;1. 使用背景2. 在vscode中配置continue2.1 vscode版本2.2 在vscode中下载continue插件2.2.1 直接进行安装2.2.2 在左下角就会有continue的按钮2.2.3 可以移动到右上角2.2.3 使用的时候需要login 2.3 配…

C++ (初始面向对象之继承,实现继承,组合,修饰权限)

初始面向对象之继承 根据面向对象的编程思路&#xff0c;我们可以把共性抽象出来封装成类&#xff0c;然后让不同的角色去继承这些类&#xff0c;从而避免大量重复代码的编写 实现继承 继承机制是面向对象程序设计中使代码可以复用的最重要的手段&#xff0c;它允许程序员在保…

每日搜索--12月

12.1 1. urlencode是一种编码方式,用于将字符串以URL编码的形式进行转换。 urlencode也称为百分号编码(Percent-encoding),是特定上下文的统一资源定位符(URL)的编码机制。它适用于统一资源标识符(URI)的编码,也用于为application/x-www-form-urlencoded MIME准备数…

一天一个java知识点----Tomcat与Servlet

认识BS架构 静态资源&#xff1a;服务器上存储的不会改变的数据&#xff0c;通常不会根据用户的请求而变化。比如&#xff1a;HTML、CSS、JS、图片、视频等(负责页面展示) 动态资源&#xff1a;服务器端根据用户请求和其他数据动态生成的&#xff0c;内容可能会在每次请求时都…

游戏报错?MFC140.dll怎么安装才能解决问题?提供多种MFC140.dll丢失修复方案

MFC140.dll 是 Microsoft Visual C 2015 运行库的重要组成部分&#xff0c;许多软件和游戏依赖它才能正常运行。如果你的电脑提示 "MFC140.dll 丢失" 或 "MFC140.dll 未找到"&#xff0c;说明系统缺少该文件&#xff0c;导致程序无法启动。本文将详细介绍 …

小白如何从0学习php

学习 PHP 可以从零开始逐步深入&#xff0c;以下是针对小白的系统学习路径和建议&#xff1a; 1. 了解 PHP 是什么 定义&#xff1a;PHP 是一种开源的服务器端脚本语言&#xff0c;主要用于 Web 开发&#xff08;如动态网页、API、后台系统&#xff09;。 用途&#xff1a;构建…

常见的 14 个 HTTP 状态码详解

文章目录 一、2xx 成功1、200 OK2、204 No Content3、206 Partial Content 二、3xx 重定向1、301 Moved Permanently2、302 Found3、303 See Other注意4、Not Modified5、307 Temporary Redirect 三、4xx 客户端错误1、400 Bad Request2、401 Unauthorized3、403 Forbidden4、4…

Linux Kernel 8

可编程中断控制器&#xff08;Programmable Interrupt Controller&#xff0c;PIC&#xff09; 支持中断&#xff08;interrupt&#xff09;的设备通常会有一个专门用于发出中断请求Interrupt ReQuest&#xff0c;IRQ的输出引脚&#xff08;IRQ pin&#xff09;。这些IRQ引脚连…

原子操作CAS(Compare-And-Swap)和锁

目录 原子操作 优缺点 锁 互斥锁&#xff08;Mutex&#xff09; 自旋锁&#xff08;Spin Lock&#xff09; 原子性 单核单CPU 多核多CPU 存储体系结构 缓存一致性 写传播&#xff08;Write Propagation&#xff09; 事务串行化&#xff08;Transaction Serialization&#…

【WPF】 在WebView2使用echart显示数据

文章目录 前言一、NuGet安装WebView2二、代码部分1.xaml中引入webview22.编写html3.在WebView2中加载html4.调用js方法为Echarts赋值 总结 前言 为了实现数据的三维效果&#xff0c;所以需要使用Echarts&#xff0c;但如何在WPF中使用Echarts呢&#xff1f; 一、NuGet安装WebV…

OpenCV 图像拼接

一、图像拼接的介绍 图像拼接是一种将多幅具有部分重叠内容的图像合并成一幅完整、无缝且具有更广阔视野或更高分辨率图像的技术。其目的是通过整合多个局部图像来获取更全面、更具信息价值的图像内容。 二、图像拼接的原理 图像拼接的核心目标是将多幅有重叠区域的图像进行准…

数学建模AI智能体(4.16大更新)

别的不说就说下面这几点&#xff0c;年初内卷到现在&#xff0c;就现阶段AI水平&#xff0c;卷出了我比较满意的作品&#xff0c;这里分享给各位同学&#xff0c;让你们少走弯路&#xff1a; 1.轻松辅导学生 2.帮助学习 3.突破知识壁垒&#xff0c;缩短与大佬的差距 4.打破…

Linux》》bash 、sh 执行脚本

通常使用shell去运行脚本&#xff0c;两种方法 》bash xxx.sh 或 bash “xxx.sh” 、sh xxx.sh 或 sh “xxx.sh” 》bash -c “cmd string” 引号不能省略 我们知道 -c 的意思是 command&#xff0c;所以 bash -c 或 sh -c 后面应该跟一个 command。

如何用“AI敏捷教练“破解Scrum项目中的“伪迭代“困局?

一、什么是“伪迭代”&#xff1f; “伪迭代”是指团队表面上采用Scrum框架&#xff0c;但实际运作仍沿用瀑布模式的现象。例如&#xff1a;迭代初期开发人员集中编码、末期测试人员突击测试&#xff0c;导致资源分配不均&#xff1b;需求拆分粗糙&#xff0c;团队无法在固定时…

使用 vxe-table 来格式化任意的金额格式,支持导出与复制单元格格式到 excel

使用 vxe-table 来格式化任意的金额格式&#xff0c;支持导出与复制单元格格式到 excel 查看官网&#xff1a;https://vxetable.cn gitbub&#xff1a;https://github.com/x-extends/vxe-table gitee&#xff1a;https://gitee.com/x-extends/vxe-table 安装 npm install vx…