首页 > 其他分享 ><二>自己实现简单的string

<二>自己实现简单的string

时间:2022-11-21 22:49:08浏览次数:51  
标签:const string 实现 rValue return char pString 简单 MyString

我们结合运算符重载知识实现string 类
在自己实现的String类中可以参考C++中string的方法
例如构造,加法,大小比较,长度,[] 等操作.
当前的MyString 类中,暂时不加入迭代器,我们将在下一节中加入迭代器的代码.

#include <iostream>
using namespace std;

class MyString {

public:

	//构造函数
	MyString(const char * pSource = nullptr) {
		if (pSource != nullptr) {
			pString = new char[strlen(pSource) + 1];
			strcpy(pString, pSource);
		}
		else {
			pString = new char[1];
			pString[0] = '\0';
		}
		cout << "MyString构造函数,对象地址="<<this << endl;
	}

	//拷贝构造
	MyString(const MyString & _rValue) {

		pString = new char[strlen(_rValue.pString) + 1];
		strcpy(pString, _rValue.pString);
		cout << "MyString拷贝构造函数" << endl;
	}

	//赋值函数
	MyString & operator=(const MyString & _rValue) {

		if (this == &_rValue) {
			return *this;
		}
		delete[] this->pString;
		this->pString = nullptr;
	
		char * _tpString = new char[strlen(_rValue.pString) + 1];
		strcpy(_tpString, _rValue.pString);
		
		cout << "MyString赋值函数" << endl;

	}

	//可编辑
	char & operator[](int index) {

		int len = strlen(this->pString);
		if (index<0) { return pString[0]; }
		else if (index>len) { return pString[index]; }
		else { return pString[index]; }

	}

	//不可编辑
	const char operator[](int index) const {

		int len = strlen(this->pString);
		if (index<0) { return pString[0]; }
		else if (index>len) { return pString[index]; }
		else { return pString[index]; }

	}

	bool operator>(const MyString & _rValue) const {
		return strcmp(this->pString, _rValue.pString)>0;
	}
	bool operator<(const MyString & _rValue) const {
		return strcmp(this->pString, _rValue.pString)<0;
	}
	bool operator==(const MyString & _rValue) const {
		return strcmp(this->pString, _rValue.pString)==0;
	}

	int length() const {
		return strlen(this->pString);
	}

	~MyString() {
		if (this->pString != nullptr) {
			delete[] this->pString;
			this->pString = nullptr;
			cout << "MyString析构函数" << this << endl;
		}
	}

	const char * c_str()  const { return this->pString; }

private:
	char * pString ;
	friend MyString operator+  (const MyString & s1, const MyString &s2) ;
	friend ostream & operator<<(ostream & out, const MyString &s) ;

};

MyString operator+(const MyString & s1, const MyString &s2) {
	
	/*
	   方式1 这段代码有 内存泄漏问题  tp 没有释放掉
	   int newLength = strlen(s1.pString) + strlen(s2.pString) + 1;
	   char *tp = new char[newLength + 1];//重新申请空间
	   strcpy(tp, s1.pString);
	   strcat(tp, s2.pString);

	   MyString s(tp);
	   cout << "operator+ = " << &s << endl;
	   return s;
	*/

	/*
	  方式2 对比方式1 效果更高
	*/
	MyString s;
	int newLength = strlen(s1.pString) + strlen(s2.pString) + 1;
	s.pString = new char[newLength + 1];//重新申请空间
	strcpy(s.pString, s1.pString);
	strcat(s.pString, s2.pString);	
	cout << "operator+ = " << &s << endl;
	return s;
}

ostream & operator<<(ostream & out, const MyString &s) {
	    cout << s.pString << endl;
		return out;
}

void test() {

	MyString s1("12345");
	MyString s2("6789ABC");

	cout << s1 << endl;
	cout << s2 << endl;

	MyString s3 = s1 + s2;
	cout << s3 << endl;
	cout << "s3 = " << &s3 << endl;

	for (int i = 0; i < s3.length(); i++) {
		cout << s3[i] << endl;
	}
	
	cout <<"--------------------" << endl;
	s3[0] = 'W';
	for (int i = 0; i < s3.length(); i++) {
		cout << s3[i] << endl;
	}

	const MyString s4("hello");
	
	for (int i = 0; i < s4.length(); i++) {
		cout << s4[i] << endl;
	}

}
int main() {

	test();

	system("pause");
	return 0;
}

标签:const,string,实现,rValue,return,char,pString,简单,MyString
From: https://www.cnblogs.com/erichome/p/16913628.html

相关文章

  • Python 多进程(一)简单场景
    需求:使用多进程,把add的结果放进list原始的多进程之间不能共享数据使用Manager来管理list,多进程可以操作同一个list使用multiprocessing.Manager().list()创建一个listd......
  • oa项目-系统通知实现
    1,采用的是mongdB存储,普通的mysql难以支撑2,mongdB搞冷热分离,但是考虑到大型web项目也难以支撑一条公告发送百万数据,采用消息队列细睡长流慢慢的写入mongdb3,使用异步线程的......
  • C/C++员工通讯录(链表实现)
    C/C++员工通讯录(链表实现)一、设计一个员工通讯录(如编号、身份证号码、姓名等),用单链表实现员工通讯录的存储和增删改查等操作。通讯录链表的建立;通讯者信息的插入;通讯......
  • 在C#中使用Irony实现SQL语句的解析
    在上一篇博文中,我介绍了LOGO语言的C#实现,在该项目中,我使用了.NET下的语法解析框架:Irony。Irony框架最开始的时候是由RomanIvantsov发布在codeplex上的一个开源项目,它使用M......
  • 【RocketMQ】顺序消息实现原理
    全局有序在RocketMQ中,如果使消息全局有序,可以为Topic设置一个消息队列,使用一个生产者单线程发送数据,消费者端也使用单线程进行消费,从而保证消息的全局有序,但是这种方式效......
  • 多进程实现TCP服务端并发、互斥锁代码实操、线程理论、创建线程的两种方式、线程的诸
    多进程实现TCP服务端并发importsocketfrommultiprocessingimportProcessdefget_server():server=socket.socket()server.bind(('127.0.0.1',8080))......
  • C++初阶(list容器+模拟实现)
    list介绍list的本质是一个带头的双向循环链表。链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列......
  • 互斥锁 线程理论 GIL全局解释器锁 死锁现象 信号量 event事件 进程池与线程池 协程实
    目录互斥锁multiprocessingLock类锁的种类线程理论进程和线程对比开线程的两种方式(类似进程)方式1使用Thread()创建线程对象方式2重写Thread类run方法创建1000个进程vs......
  • C# 简单实现线程池
    NET6环境开发 实现线程数量,任务队列,非核心线程,及核心线程活跃时间的管理。namespaceCustomThreadPool;///<summary>///线程池类///</summary>publicclassT......
  • springboot实现无数据库启动
    问题springboot往往是作为b/s系统的server端的架子来使用,但是有些时候,是作为静默的server,并没有界面和数据库,但是springboot默认是链接数据库的,如何解决这个问题呢?使用sprin......