自己写的string类

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
#include<cctype>
using namespace std;
class String {
	//符号重载(通过友元函数)
	friend ostream& operator<<(ostream& os, String& str);//重载流插入运算符
	friend istream& operator>>(istream& is, String& str);//重载流提取运算符
	friend String operator+(String& s1, String& s2);//s1+s2;
	friend String operator+(String& s1, char* p);//s1+char* p
	friend String operator+(char* p, String& s1);
	friend bool operator==(const String& s1, const String& s2);
	friend bool operator<(const String& s1, const String& s2);
	friend bool operator>(const String& s1, const String& s2);
	friend bool operator!=(const String& s1, const String& s2);
private:
	char* ptr;
	int len;
public:
	String();
	~String();//析构函数
	int find(char ch);
	String& operator=(const char* p);
	String& operator=(const String& another);
	char& operator[](int i);
	int size();
};
String::String() {
	ptr = new char[1];
	ptr[0] = 0;
	len = 0;
}
String::~String() {
	delete[]ptr;
}
int String::find(char ch) {
	for (int i = 0; i < strlen(this->ptr); i++) {
		if (this->ptr[i] == ch) {
			return i;
		}
	}
	return -1;
}
String& String::operator=(const char* p) {
	delete[]ptr;
	ptr = new char[strlen(p) + 1];
	len = strlen(p);
	strcpy(ptr, p);
	return *this;
}
String& String::operator=(const String& another) {
	if (this == &another) {
		return *this;
	}
	else {
		if (this->ptr != NULL) {
			delete[]this->ptr;
			this->ptr = NULL;
			this->len = 0;
		}//深拷贝
		this->ptr = new char[strlen(another.ptr) + 1];
		strcpy(this->ptr, another.ptr);
		return *this;
	}
}
char& String::operator[](int i) {
	if (i >= len) {
		cout << "WARNING:the arry is overflow !!!!!!" << endl;
	}
	else return ptr[i];//注意返回值为:一个对于char变量的引用!!!
}
int String::size() {
	return strlen(this->ptr) - 1;//获取字符串大小.
}
istream& operator>>(istream& is, String& str) {
	if (str.ptr != NULL) {
		delete[]str.ptr;
		str.ptr = NULL;
		str.len = 0;
	}
	char s[1001] = { '0' };
	cin >> s;
	str.len = strlen(s);
	str.ptr = new char[str.len + 1];//动态内存分配
	strcpy(str.ptr, s);
	return is;
}
ostream& operator<<(ostream& os, String& str) {
	os << str.ptr;
	return os;
}
String operator+(String& s1, char* p) {
	String str;
	char* ppd = new char[strlen(s1.ptr) + strlen(p) + 1];
	strcpy(ppd, s1.ptr);
	strcat(ppd, p);
	str.len = strlen(s1.ptr) + strlen(p);
	strcpy(str.ptr, ppd);
	return str;
}
String operator+(String& s1, String& s2) {
	String str;
	str.ptr = new char[strlen(s1.ptr) + strlen(s2.ptr) + 1];
	str.len = strlen(s1.ptr) + strlen(s2.ptr);
	strcpy(str.ptr, s1.ptr);
	strcat(str.ptr, s2.ptr);
	return str;
}
String operator+(char* p, String& s1) {
	String str;
	char* ppd = new char[strlen(s1.ptr) + strlen(p) + 1];
	strcpy(ppd, s1.ptr);
	strcat(ppd, p);
	str.len = strlen(s1.ptr) + strlen(p);
	strcpy(str.ptr, ppd);
	return str;
}
bool operator==(const String& s1, const String& s2) {
	if (strlen(s1.ptr) != strlen(s2.ptr)) return false;
	else {
		for (int i = 0; i < strlen(s1.ptr); i++) {
			if (s1.ptr[i] != s2.ptr[i]) return false;
		}
		return true;
	}
}
bool operator!=(const String& s1, const String& s2) {
	if (strlen(s1.ptr) != strlen(s2.ptr)) return true;
	else {
		for (int i = 0; i < strlen(s1.ptr); i++) {
			if (s1.ptr[i] == s2.ptr[i]) return false;
		}
		return true;
	}
}
int main()
{
	String s1, s2, s3;
	s1 = "hello";
	s2 = "world";
	s3 = s1 + s1;
	cout << s3;
	system("pause");
	exit(0);
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值