C++mystring功能增加【C++】(w)

mustring.cpp

#define _CRT_SECURE_NO_WARNINGS
#include "mystring.h"
#include <string.h>
#include <iostream>

using namespace std;

mystring::mystring(const char* s)//构造器实现
{
	if (s == nullptr)
	{
		_str = new char[1];     //默认参数做标志位
		*_str = '\0';
	}
	else
	{
		_str = new char[strlen(s) + 1];
		strcpy(_str, s);
	}
}

mystring::~mystring()        //析构器实现
{
	delete[]_str;
}

mystring & mystring::operator = (const mystring & another)
{
	//this    another
	if (this == &another)
	{
		return *this;
	}
		delete[]this->_str;
		_str = new char[strlen(another._str) + 1];
		strcpy(_str, another._str);
		return *this;
}
mystring::mystring(const mystring & another)   //拷贝构造实现
{
	_str = new char[strlen(another._str) + 1];
	strcpy(_str, another._str);
}
		
void mystring::dump()
{
	cout << _str << endl;
}


bool mystring::operator ==(const mystring & another)
{
	return strcmp(this->_str,another._str) == 0;
}
bool mystring::operator > (const mystring & another)
{
	return strcmp(this->_str, another._str) > 0;
}
bool mystring::operator <(const mystring & another)
{
	return strcmp(this->_str, another._str) < 0;
}
bool mystring::operator >=(const mystring & another)
{
	return strcmp(this->_str, another._str) >= 0;
}
bool mystring::operator <=(const mystring & another)
{
	return strcmp(this->_str, another._str) <= 0;
}

mystring mystring::operator +(const mystring & another)  //栈对象的返回
{
	int len = strlen(this->_str) + strlen(another._str);
	mystring ms;
	delete []ms._str;
	ms._str = new char[len + 1]{0};
	strcat(strcat(ms._str, this->_str), another._str);
	return ms;
}
mystring& mystring::operator += (const mystring& another)
{
	int catLen = strlen(this->_str); 
	int srcLen = strlen(another._str); 
	int len = catLen + srcLen; 
	this->_str = static_cast<char*>(realloc(this->_str, len + 1));
	memset(this->_str + catLen, 0, srcLen + 1);
	strcat(this->_str, another._str);
	return *this;
}


char mystring:: at(int n)
{
	if (n > strlen(_str))
		exit(0);
	return _str[n];
}

char  mystring::operator[](int n)
{
	return _str[n];
}

char* mystring::c_str()
{
	return _str;
}

mustring.h

#pragma once


class mystring
{
public:
	mystring(const char* s = nullptr);   //构造器
	void dump();   //倾倒函数
	~mystring();   //析构器
	mystring & operator = (const mystring & another); //复制运算符重载
	mystring(const mystring & another);    //拷贝构造
	bool operator == (const mystring & another);
	bool operator > (const mystring & another);
	bool operator < (const mystring & another);
	bool operator >= (const mystring& another);
	bool operator <= (const mystring& another);

	mystring operator + (const mystring& another);
	mystring& operator += (const mystring& another);

	char at(int n);
	char operator[](int n);
	char *c_str();
private:
	char * _str;
};

main.cpp

#include "mystring.h"
#include <iostream>
#include <string>
using namespace std;


int main()
{
	cout << "string 实现:" << endl;  //标准string	  
	string s;        //无参构造     空串  ""   大小为1
	string s1("china");   //有参构造   非空串"china"    大小为 5+1
	cout << s << endl;     //输出内容为空
	cout << s1 << endl;	   //输出china
	string s3(s1);
	cout << s3 << endl;
	string s4("china");
	string s5("victory");
	if (s4 == s5)           //字符串相等比较
		cout << "==" << endl;
	if (s4 >= s5)           //字符串大于比较
		cout << ">=" << endl;
	if (s4 <= s5)           //字符串小于比较
		cout << "<=" << endl;
	string s8 = s4 + s5;
	cout << s8 << endl;
	(s4 += s5) = "world";      //表达式有返回值并且可以被赋值
	cout << s4 << endl;
	cout << s4.at(3) << endl;
	cout << s4[3] << endl;
	cout << s4.operator[](3)<< endl;
	cout << s4.c_str() << endl;
	cout << "==============================" << endl;
	cout << "mystring 实现:" << endl;  //自实现string	
	mystring ms;
	mystring ms1("china");
	ms.dump();
	ms1.dump();
	mystring ms3(ms1);
	ms3.dump();
	mystring s6("china");
	mystring s7("victory");
	if (s6 == s7)           //自实现字符串相等比较
		cout << "==" << endl;
	if (s6 >= s7)            //自实现字符串大于比较
		cout << ">=" << endl;
	if (s6 <= s7)            //自实现字符串小于比较
		cout << "<=" << endl;
	mystring s9 = s6 + s7;
	s9.dump();
	cout << "===================================" << endl;
	s6 += s7;
	s6.dump();
	(s6 += s7) = "abc";   //表达式有返回值并且可以被赋值
	s6.dump();
	cout << ms1.at(3) << endl;  //打印字符中序号为3的字符
	cout << ms1.operator[](3) << endl;  //打印字符中序号为3的字符
	cout << ms1.c_str() << endl;
	return 0;
}

测试结果为:

在这里插入图片描述

发布了163 篇原创文章 · 获赞 94 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43648751/article/details/104484576