C++ 字符串类 简易封装

MyString.h

#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <iostream>
using namespace std;

class MyString
{
	friend ostream& operator<< (ostream  & out, MyString& str);
	friend istream& operator>>(istream& in, MyString& str);

public:
	MyString(const char *);
	MyString(const MyString&);
	~MyString();

	char& operator[](int index); //[]重载

	//=号重载
	MyString& operator=(const char * str);
	MyString& operator=(const MyString& str); 

	//字符串拼接 重载+号
	MyString operator+(const char * str );
	MyString operator+(const MyString& str);

	//字符串比较
	bool operator== (const char * str);
	bool operator== (const MyString& str);
private:
	char * pString; //指向堆区空间
	int m_Size; //字符串长度 不算'\0'
};

MyString.cpp

#include "MyString.h"

//左移运算符
ostream& operator<< (ostream & out, MyString& str)
{
	out << str.pString;
	return out;
}
//右移运算符
istream& operator>>(istream& in, MyString& str)
{
	//先将原有的数据释放
	if (str.pString != NULL)
	{
		delete[] str.pString;
		str.pString = NULL;
	}
	char buf[1024]; //开辟临时的字符数组,保存用户输入内容
	in >> buf;

	str.pString = new char[strlen(buf) + 1];
	strcpy(str.pString, buf);
	str.m_Size = strlen(buf);

	return in;
}

//构造函数
MyString::MyString(const char * str)
{
	this->pString = new char[strlen(str) + 1];
	strcpy(this->pString, str);
	this->m_Size = strlen(str);
}

//拷贝构造
MyString::MyString(const MyString& str)
{
	this->pString = new char[strlen(str.pString) + 1];
	strcpy(this->pString, str.pString);
	this->m_Size = str.m_Size;
}
//析构函数
MyString::~MyString()
{
	if (this->pString!=NULL)
	{
		delete[]this->pString;
		this->pString = NULL;
	}
}

char& MyString::operator[](int index)
{
	return this->pString[index];
}

MyString& MyString::operator=(const char * str)
{
	if (this->pString != NULL){
		delete[] this->pString;
		this->pString = NULL;
	}
	this->pString = new char[strlen(str) + 1];
	strcpy(this->pString, str);
	this->m_Size = strlen(str);
	return *this;
}

MyString& MyString::operator=(const MyString& str)
{
	if (this->pString != NULL){
		delete[] this->pString;
		this->pString = NULL;
	}
	this->pString = new char[strlen(str.pString) + 1];
	strcpy(this->pString, str.pString);
	this->m_Size = str.m_Size;
	return *this;
}


MyString MyString::operator+(const char * str)
{
	int newsize = this->m_Size + strlen(str) + 1;
	char *temp = new char[newsize];
	memset(temp, 0, newsize);
	strcat(temp, this->pString);
	strcat(temp, str);

	MyString newstring(temp);
	delete[] temp;

	return newstring;
}

MyString MyString::operator+(const MyString& str)
{
	int newsize = this->m_Size + str.m_Size + 1;
	char *temp = new char[newsize];
	memset(temp, 0, newsize);
	strcat(temp, this->pString);
	strcat(temp, str.pString);

	MyString newstring(temp);
	delete[] temp;
	return newstring;
}

bool MyString::operator==(const char * str)
{
	if (strcmp(this->pString, str) == 0 && strlen(str) == this->m_Size){
		return true;
	}

	return false;
}

bool MyString::operator==(const MyString& str)
{
	if (strcmp(this->pString, str.pString) == 0 && str.m_Size == this->m_Size){
		return true;
	}

	return false;
}

TestMyString.cpp

void test01()
{
	MyString str("hello World");

	cout << str << endl;

	//cout << "请输入MyString类型字符串:" << endl;
	//cin >> str;

	//cout << "字符串为: " << str << endl;

	//测试[]
	cout << "MyString的第一个字符为:" << str[0] << endl;

	//测试 =
	MyString str2 = "^_^";
	MyString str3 = "";
	str3 = "aaaa";
	str3 = str2;
	cout << "str2 = " << str2 << endl;
	cout << "str3 = " << str3 << endl;

	//测试 +
	MyString str4 = "我爱";
	MyString str5 = "北京";
	MyString str6 = str4 + str5;
	MyString str7 = str6 + "天安门";

	cout << str7 << endl;

	//测试 == 
	if (str6 == str7)
	{
		cout << "s6 与 s7相等" << endl;
	}
	else
	{
		cout << "s6 与 s7不相等" << endl;
	}
}
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设计 C++ 字符串的主要考虑因素包括字符串的存储方式、操作方法、性能、安全等。 以下是一种可能的设计: ```c++ class MyString { public: MyString(); // 默认构造函数 MyString(const char* str); // 从 C-style 字符串构造 MyString(const MyString& other); // 拷贝构造函数 ~MyString(); // 析构函数 MyString& operator=(const MyString& other); // 拷贝赋值运算符 // 重载下标操作符 char& operator[](size_t index); const char& operator[](size_t index) const; // 获取字符串长度 size_t length() const; // 获取 C-style 字符串指针 const char* c_str() const; // 拼接字符串 MyString operator+(const MyString& other) const; MyString& operator+=(const MyString& other); // 比较字符串 bool operator==(const MyString& other) const; bool operator!=(const MyString& other) const; bool operator<(const MyString& other) const; bool operator>(const MyString& other) const; bool operator<=(const MyString& other) const; bool operator>=(const MyString& other) const; private: char* m_data; // 字符串数据存储 size_t m_length; // 字符串长度 }; ``` 其中,`m_data` 是字符串数据的存储,可以使用 `new` 运算符在堆上分配内存,需要在析构函数中使用 `delete[]` 释放内存。`m_length` 表示字符串的长度。 在构造函数和拷贝构造函数中,需要分配新的内存,并将传入的字符串复制到 `m_data` 中。在拷贝赋值运算符中,需要先释放 `m_data` 中的内存,再重新分配并复制传入的字符串。 重载下标操作符可以方便地获取字符串中的某个字符。 `length()` 可以返回字符串长度,`c_str()` 可以返回 C-style 字符串指针。 `operator+` 和 `operator+=` 可以进行字符串拼接操作。 比较操作符可以用于字符串的比较,包括相等性、大小关系等。 需要注意的是,在设计字符串时,需要考虑到安全问题,避免出现缓冲区溢出等问题。因此,在实现字符串操作时需要对输入参数进行检查和验证。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值