c++中简单地实现自己的string类

#include <iostream>
#include <cstring>
using namespace std;

class MyString{
		// 重载运算符==
		friend bool operator==(const MyString& str1, const MyString& str2);
		// 重载运算符!=
		friend bool operator!=(const MyString& str1, const MyString& str2);	
		// 重载运算符<<
		friend ostream& operator<<(ostream& os,const MyString& str);	
		// 重载运算符>>
		friend istream& operator>>(istream& is, MyString& str);
	public:
		// 构造函数 
		MyString(const char* m_data = 0);
		// 拷贝构造函数
		MyString(const MyString& str);
		// 拷贝赋值运算符 
		MyString& operator=(const MyString& str);
		// 移动构造函数
		MyString(MyString&& str) noexcept;	
		// 移动赋值运算符
		MyString&  operator=(MyString&& str) noexcept;
		// 重载运算符+ 
		MyString operator+(const MyString& str);
		// 重载运算符+=
		MyString& operator+=(const MyString& str);
		// 重载运算符[]
		char& operator[](int index);
		// 获得长度
		int getSize();
		// 获得原始指针
		char* get();
		// 析构函数
		~MyString();
	private:
		char *data;	
};

// 构造函数 
MyString::MyString(const char* m_data)
{
	if(m_data){
		data = new char[strlen(m_data)+1];
		strcpy(data,m_data);
	}else{
		data = new char[1];
		*data = '\0';    
	}
}

// 拷贝构造函数
MyString::MyString(const MyString& str)
{
	data = new char[strlen(str.data)+1];
	strcpy(data, str.data);
}

// 拷贝赋值运算符 
MyString& MyString::operator=(const MyString& str)
{
	if(this != &str){
		delete []data;
		data = new char[strlen(str.data)+1];
		strcpy(data, str.data);
	}
	return *this;
} 

// 移动构造 
MyString::MyString(MyString&& str) noexcept:data(str.data)
{
	str.data = nullptr;
}

// 移动赋值运算符		
MyString&  MyString::operator=(MyString&& str) noexcept
{
	if(this != &str){
		delete []data;
		data = str.data;
		str.data = nullptr;
	}
	return *this;
}

// 重载运算符+ 
MyString MyString::operator+(const MyString& str){
	auto len = strlen(data) + strlen(str.data) + 1;
	MyString temp;
	temp.data = new char[len];
	strcpy(temp.data,this->data);
	strcat(temp.data,str.data);
	return temp;	
} 

// 重载运算符+=
MyString& MyString::operator+=(const MyString& str)
{	
	*this = *this + str;
	return *this;
} 

// 重载运算符==
bool operator==(const MyString& str1, const MyString& str2)
{	
	int ret = strcmp(str1.data, str2.data);
	if(ret == 0)
		return true;
	return false;
} 

// 重载运算符!=
bool operator!=(const MyString& str1, const MyString& str2)
{
	return !(str1 == str2);
} 

// 重载运算符<<
ostream& operator<<(ostream& os,const MyString& str)
{
	os << str.data;
	return os;
} 

// 重载运算符>>
istream& operator>>(istream& is, MyString& str)
{
	if(str.data){
		delete str.data;
		str.data = nullptr;
	}
	char temp[2048];
	is >> temp;
	str.data = new char[strlen(temp)+1];
	strcpy(str.data, temp);
	return is;
}

// 重载运算符[]
char& MyString::operator[](int index)
{
	return data[index];
} 


// 获得长度
int MyString::getSize()
{
	return strlen(data);
} 

// 获得原始指针
char* MyString::get()
{
	return data;
} 

// 析构函数
MyString::~MyString()
{
	if(data) 
		delete []data;
} 	
	
int main(void)
{
	// 测试<<和>>
	cout << "测试<<和>>,请输入字符串:" << endl; 
	MyString s0;
	cin >> s0;
	cout << s0 << endl ;
	 
	cout << endl << "测试其他功能:" << endl; 
	// 测试构造函数 
	MyString s1("hello");
	cout << s1 << endl;
	char s[] = {"hi"};
	MyString s2(s);
	cout << s2 << endl;
	
	// 测试拷贝构造函数
	MyString s3 = s2;
	cout << s3 << endl;
	
	// 测试拷贝赋值运算符
	s3 = s1;
	cout << s3 << endl;
	
	// 测试== 和 != 
	if(s1 == s2)
		cout << "s1与s2相等" << endl;
	else 
		cout << "s1与s2不相等"  << endl;
	
	if(s1 != s2)
		cout << "s1与s2不相等" << endl;
	else 
		cout << "s1与s2相等"  << endl;
	
	// 测试+和+=	
	s1 = s1 + s2;
	cout << s1 << endl;
	
	s1 += s2;
	cout << s1 << endl;
	
	// 测试[]运算符
	cout << s1[4] << endl; 
	
	return 0;	
} 

结果
在这里插入图片描述
注:还在完善中,如有错误,还请指正。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值