string源码解析

1、String源码解释与测试

调试环境采用的是VS2015,大家可以选择自己合适的编译环境去调试。
这边用了几个C语言的库函数是为了方便一点:例如strcpy,strcat等等
String.h

#ifndef STRING_H
#define STRING_H
#include<iostream>
#include<iomanip>
using namespace std;
class String {
private:
	char* _data;
public:
	String(const char* str = nullptr);
	String(const String& rhs) :_data(new char[rhs.size() + 1]) {
		strcpy(_data, rhs._data);
	}
	String& operator=(const String& rhs);
	~String() {
		if (_data != nullptr) {
			delete[]_data;
			_data = nullptr;
		}
		//delete[]_data;
	}
	void Swap(String rhs) {
		std::swap(_data, rhs._data);
	}
	size_t size() const {
		return strlen(_data);
	}
private:
	//重载操作符
	char&  operator[](int index);
	friend ostream& operator<<(ostream &os, const String &rhs);
	friend istream& operator >> (istream &is, String &rhs);
	friend String operator+(const String& lhs, const String& rhs);
	friend String operator+=(String& lhs, const String& rhs);   //注意这里不能声明成const
	friend bool operator==(const String& lhs, const String& rhs);
	friend bool operator!=(const String& lhs, const String& rhs);
	friend bool operator>(const String &lhs, const String &rhs);
	friend bool operator>=(const String &lhs, const String &rhs);
	friend bool operator<(const String &lhs, const String &rhs);
	friend bool operator<=(const String &lhs, const String &rhs);
};
String::String(const char* str) {       //构造函数,默认的输入为空
	if (str == nullptr) {
		_data = new char[1];
		*_data = '\0';
	}
	else {
		_data = new char[strlen(str) + 1];
		strcpy(_data, str);
	}
}
//
String& String::operator=(const String& rhs) {
	if (this == &rhs) {
		return *this;
	}
	delete[]_data;
	_data = new char[rhs.size() + 1];
	strcpy(_data, rhs._data);
	return *this;
}
/*
其他的写法:
//2. copy and swap技术
String& operator=(const String& rhs) {
String temp(rhs);       //利用复制构造函数
Swap(rhs);
return *this;
}
//3. 传值方式
String& operator=(String rhs) {
Swap(rhs);
return *this;
}
//4. c++11右值引用
String& operator= (String&& rhs) {
Swap(rhs);
return *this;
}
*/
char& String::operator[](int index) {
	return _data[index];
}
ostream& operator<<(ostream &os, const String &rhs) {
	os << rhs._data;
	return os;
}


istream& operator >> (istream &is, String &rhs)
{
	char temp[255]; //用于存储输入流
	is >> setw(255) >> temp;
	rhs = temp; //使用赋值运算符
	return is; //使用return可以支持连续使用>>运算符
}

//重载+
String operator+(const String& lhs, const String& rhs) {
	String res;
	int len = lhs.size() + rhs.size();
	res._data = new char[len + 1];
	strcpy(res._data, lhs._data);
	strcat(res._data, rhs._data);
	return res;
}
//重载+=
String operator+=(String& lhs, const String& rhs) {
	lhs = lhs + rhs;    //利用加法即可完成 
	return lhs;
}
//重载==
bool operator==(const String& lhs, const String& rhs) {
	if (strcmp(lhs._data, rhs._data) == 0) return true;
	return false;
}
bool operator!=(const String& lhs, const String& rhs) {
	if (strcmp(lhs._data, rhs._data) == 0) return false;
	return true;
}
//重载>函数  
bool operator>(const String &lhs, const String &rhs) {
	if (strcmp(lhs._data, rhs._data) > 0)
		return true;
	return false;
}
bool operator<(const String &lhs, const String &rhs) {
	if (strcmp(lhs._data, rhs._data) < 0)
		return true;
	return false;
}
//重载<=函数  
bool operator<=(const String &lhs, const String &rhs) {
	if (strcmp(lhs._data, rhs._data) <= 0)
		return true;
	return false;
}

//重载>=函数  
bool operator>=(const String &lhs, const String &rhs) {
	if (strcmp(lhs._data, rhs._data) >= 0)
		return true;
	return false;
}
#endif // !STRING_H

String.cpp

	#define _CRT_SECURE_NO_WARNINGS
	#include <iostream>
	#include "String.h"
	int main() 
	{
		String s;
		cout << s << endl;
		String s1 = "test";
		cout << s1 << endl;
		String s2(s1);
		cout << s2 << endl;
		s = s2;
		cout << s << endl;
		String s3;
		cout << "Please input a string" << endl;
		cin >> s3;
		cout << s3 << endl;


		String s4 = "hello";
		String s5 = "world";
		String s6 = s4 + s5;
		cout << s6 << endl;
		s6 += s4;
		cout << s6 << endl;
		cout << (s6 == s4) << endl;
		String s7(s6);
		cout << (s6 == s7) << endl;
		//
		cout << (s4 > s5) << endl;
		cout << (s6 > s5) << endl;  
		cout << (s6 > s4) << endl;
		system("pause");
		return 0;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值