用C++自定义String类

一、头文件

cpp 复制代码
#pragma once
#include <iostream>

/**
 * 自定义String类
 */
class String 
{
public:
	String();
	String(const char *pstr);
	String(const String &str);
	~String();
	String &operator= (const String &str);
	String &operator= (const char *pstr);

	String &operator+= (const String &str);
	String &operator+= (const char *pstr);
	
	char &operator[] (std::size_t index);
	const char &operator[] (std::size_t index) const;
	
	std::size_t size() const;
	const char* c_str() const;
	
	friend bool operator== (const String &leftStr, const String &rightStr);
	friend bool operator!= (const String &leftStr, const String &rightStr);
	
	friend bool operator< (const String &leftStr, const String &rightStr);
	friend bool operator> (const String &leftStr, const String &rightStr);
	friend bool operator<= (const String &leftStr, const String &rightStr);
	friend bool operator>= (const String &leftStr, const String &rightStr);
	
	friend std::ostream &operator<< (std::ostream &os, const String &str);
    friend std::istream &operator>> (std::istream &is, String &str);

private:
	char * _pstr;
};

String operator+(const String &thisStr, const String &otherStr);
String operator+(const String &thisStr, const char *otherpStr);
String operator+(const char *thisPstr, const String &otherStr);

二、String实现

cpp 复制代码
#include <cstring>
#include "08_MyString.hh"

using namespace std;

/**
 * 无参构造函数
*/
String::String()
: _pstr(new char[1]{0}) 
{
	strcpy(_pstr, "");	
}

/**
 * 有参构造函数
*/
String::String(const char *pstr) 
: _pstr(new char[strlen(pstr) + 1]{0})
{
	strcpy(_pstr, pstr);
}

/**
 * 拷贝构造函数
*/
String::String(const String &str)
: _pstr(new char[strlen(str._pstr) + 1]{0})
{
	strcpy(_pstr, str._pstr);
}

/**
 * 析构函数
*/
String::~String() {
	delete [] _pstr;
}

/**
 * 重载赋值运算符函数
*/
String &String::operator=(const String &str) {
	if (this != &str) { // 跳过自赋值
		delete [] _pstr;
		_pstr = new char[strlen(str._pstr) + 1]{0};
		strcpy(_pstr, str._pstr);
	} 
	return *this; 
}
String &String::operator=(const char *pstr) {
	delete [] _pstr;
	_pstr = new char[strlen(pstr) + 1]{0};
	strcpy(_pstr, pstr);
	return *this;
}

/**
 * 重载+=
*/
String &String::operator+= (const String &str) {
	size_t newLen = strlen(_pstr) + strlen(str._pstr) + 1;
	char *newStr = new char[newLen]{0};
	strcat(newStr, _pstr);
	strcat(newStr, str._pstr);
	delete [] _pstr;
	_pstr = newStr;
	return *this;
}
String &String::operator+= (const char *pstr) {
	size_t newLen = strlen(_pstr) + strlen(pstr) + 1;
	char *newStr = new char[newLen]{0};
	strcat(newStr, _pstr);
	strcat(newStr, pstr);
	delete [] _pstr;
	_pstr = newStr;
	return *this;
}

/**
 * 重载下标运算符
*/
char &String::operator[] (size_t index) {
	if (index < strlen(_pstr)) { // 未越界
		return _pstr[index];
	}else {
		static char nullchar = '\0';
		return nullchar;
	}	
}
const char &String::operator[] (size_t index) const {
	if (index < strlen(_pstr)) { // 未越界
		return _pstr[index];
	}else {
		static char nullchar = '\0';
		return nullchar;
	}	
}

/**
 * 获取字符串长度
*/
size_t String::size() const {
	return strlen(_pstr);
}

/**
 * 返回C风格字符串
*/
const char *String::c_str() const {
	return _pstr;
}

/**
 * 重载==
*/
bool operator==(const String &leftStr, const String &rightStr) {
	return strcmp(leftStr._pstr, rightStr._pstr) == 0 ? true : false;
}
/**
 * 重载!=
*/
bool operator!=(const String &leftStr, const String &rightStr) {
	return strcmp(leftStr._pstr, rightStr._pstr) != 0 ? true : false;
}
/**
 * 重载<
*/
bool operator<(const String &leftStr, const String &rightStr) {
	return strcmp(leftStr._pstr, rightStr._pstr) < 0 ? true : false;
}
/**
 * 重载>
*/
bool operator>(const String &leftStr, const String &rightStr) {
	return strcmp(leftStr._pstr, rightStr._pstr) > 0 ? true : false;
}
/**
 * 重载<=
*/
bool operator<=(const String &leftStr, const String &rightStr) {
	int res = strcmp(leftStr._pstr, rightStr._pstr); 
	return res > 0 ? false : true;
}
/**
 * 重载>=
*/
bool operator>=(const String &leftStr, const String &rightStr) {
	int res = strcmp(leftStr._pstr, rightStr._pstr); 
	return res < 0 ? false : true;
}

/**
 * 重载输出流函数
*/
ostream &operator<< (ostream &os, const String &str) {
	os << str._pstr;
	return os;
}

/**
 * 重载输入流函数
*/
istream &operator>> (istream &is, String &str) {
	char buf[4096];
	is.getline(buf, sizeof(buf));
	delete [] str._pstr;
	str._pstr = new char[strlen(buf) + 1];
	strcpy(str._pstr, buf);
	return is;
}

/**
 * 重载+
*/
String operator+(const String &thisStr, const String &otherStr) {
	String temp(thisStr.c_str());
	temp+=otherStr.c_str();
	return temp;
}
String operator+(const String &thisStr, const char *otherPstr) {
	String temp(thisStr.c_str());
	temp+=otherPstr;
	return temp;
}
String operator+(const char *thisPstr, const String &otherStr) {
	String temp(thisPstr);
	temp+=otherStr.c_str();
	return temp;
}
相关推荐
虾球xz1 小时前
游戏引擎学习第216天
服务器·c++·学习·游戏引擎
星星火柴9362 小时前
数据结构:哈希表 | C++中的set与map
数据结构·c++·笔记·算法·链表·哈希算法·散列表
没有啥的昵称4 小时前
从源码安装ROS的serial包(替换github的方案)
c++
夏天的阳光吖5 小时前
C++蓝桥杯实训篇(四)
开发语言·c++·蓝桥杯
小乐xiaole6 小时前
蓝桥杯 2025 C++组 省 B 题解
c++·蓝桥杯·深度优先
晓纪同学6 小时前
C++ Primer (第五版)-第十三章 拷贝控制
java·开发语言·c++
arriettyandray7 小时前
Qt/C++学习系列之QTreeWidget的简单使用记录
c++·qt·学习
zhaoyqcsdn8 小时前
C++对象池设计:从高频`new/delete`到性能飞跃的工业级解决方案
c++·经验分享·笔记
CppPlayer-程序员阿杜8 小时前
poll为什么使用poll_list链表结构而不是数组 - 深入内核源码分析
网络·c++·链表·list·poll
@hdd8 小时前
C++| 深入剖析std::list底层实现:链表结构与内存管理机制
c++·链表·list