自定义String类

本文档介绍了如何在C++中自定义一个String类,包括头文件的声明、源文件的实现,以及对应的测试案例和测试结果展示,旨在深入理解C++字符串处理和类的设计原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

头文件

#pragma once

#include <iostream>

class String
{
   
public:
	String();
	String(const char *);
	String(const String &);
	virtual ~String();

	char & operator[](int) const;
	String & operator+(const String &);
	String & operator+(const char *);
	String & operator+(const char);
	String & operator+=(const String &);
	String & operator+=(const char *);
	String & operator+=(const char);
	bool operator<(const String &);
	bool operator>(const String &);
	bool operator==(const String &);
	friend std::ostream & operator<<(std::ostream &, const String &);
	friend std::istream & operator>>(std::istream &, const String &);

	int length() const;
	int size() const;
	int capacity() const;

	void push_back(const String &);
	void push_back(const char *);
	void push_back(const char);
	void pop_back();
	void push_front(const String &);
	void push_front(const char *);
	void push_front(const char);
	friend void swap(String &, String &);
	friend void reverse(String &);
	friend int stoi(String &);
	friend void erase(String &);

	void print_mystring() const;
private:
	char * m_str = NULL;
	int m_size = 0;
	int m_capacity = 0;
	static const int m_cap = 15;
};

String to_string(int);
String to_string(unsigned int);
String to_string(float);
String to_string(double);

源文件

#include "MyString.h"

String::String()
{
   
	printf("无参构造函数\n");
	m_size = 0;
	m_capacity = m_cap;
	m_str = new char[1];
	m_str[0] = '\0';
}

String::String(const char * str)
{
   
	printf("有参构造函数\n");
	m_size = strlen(str);
	m_capacity = m_size % m_cap > 0 ? m_size / m_cap + 1 : m_size / m_cap;
	m_capacity = m_cap * (m_size == 0 ? 1 : m_capacity);
	m_str = new char[m_capacity + 1];
	memset(m_str, 0, m_capacity);
	memcpy(m_str, str, m_size);
	m_str[m_size] = '\0';
}

String::String(const String & str)
{
   
	printf("拷贝构造函数\n");
	m_size = str.m_size;
	m_capacity = str.m_capacity;
	m_str = new char[m_capacity + 1];
	memset(m_str, 0, m_capacity);
	memcpy(m_str, str.m_str, m_size);
	m_str[m_size] = '\0';
}

String::~String()
{
   
	printf("析构函数\n");
}

char & String::operator[](int index) const
{
   
	// TODO: 在此处插入 return 语句
	if (this->m_size <= index)
	{
   
		throw;
	}
	return *(m_str + index);
}

String & String::operator+(const String & str)
{
   
	if (m_size + str.m_size >= m_capacity)
	{
   
		m_capacity += str.m_capacity;
		char * temp = new char[m_capacity + 1];
		memset(temp, 0, m_capacity);
		memcpy(temp, m_str, m_size);
		memcpy(temp + m_size, str.m_str, str.m_size);
		m_size += str.m_size;
		temp[m_size] = '\0';
		m_str = temp;
	}
	else
	{
   
		memcpy(m_str + m_size, str.m_str, str.m_size);
		m_size += str.m_size;
		m_str[m_size] = '\0';
	}

	return *this;
}

String & String::operator+(const char * str)
{
   
	// TODO: 在此处插入 return 语句
	int slen = strlen(str);
	if (m_size + slen >= m_capacity)
	{
   
		m_capacity = (m_size + slen) % m_cap > 0 ? (m_size + slen) / m_cap + 1 : (m_size + slen) / m_cap;
		m_capacity = m_cap * ((m_size + slen) == 0 ? 1 : m_capacity);
		char * temp = new char[m_capacity + 1];
		memset(temp, 0, m_capacity);
		memcpy(temp, m_str, m_size);
		memcpy(temp + m_size, str, slen);
		m_size += slen;
		temp[m_size] = '\0';
		m_str = temp;
	}
	else
	{
   
		memcpy(m_str + m_size, str, slen);
		m_size += slen;
		m_str[m_size] = '\0';
	}

	return *this;
}

String & String::operator+(const char chr)
{
   
	// TODO: 在此处插入 return 语句
	if (m_size + 1 >= m_capacity)
	{
   
		m_capacity = (m_size + 1) % m_cap > 0 ? (m_size + 1) / m_cap + 1 : (m_size + 1) / m_cap;
		m_capacity = m_cap * ((m_size + 1) == 0 ? 1 : m_capacity);
		char * temp = new char[m_capacity + 1];
		memset(temp, 0, m_capacity);
		memcpy(temp, m_str, m_size);
		memcpy(temp + m_size, &chr, 1);
		m_size += 1;
		temp[m_size] = '\0';
		m_str = temp;
	}
	else
	{
   
		memcpy(m_str + m_size, &chr, 1);
		m_size += 1;
		m_str[m_size] = '\0';
	}

	return *this;
}

String & String::operator+=(const String & str)
{
   
	// TODO: 在此处插入 return 语句
	if (m_size + str.m_size >= m_capacity)
	{
   
		m_capacity += str.m_capacity;
		char * temp = new char[m_capacity + 1];
		memset(temp, 0, m_capacity);
		memcpy(temp, m_str, m_size);
		memcpy(temp + m_size, str.m_str, str.m_size);
		m_size += str.m_size;
		temp[m_size] = '\0';
		m_str = temp;
	}
	else
	{
   
		memcpy(m_str + m_size, str.m_str, str.m_size);
		m_size += str.m_size;
		m_str[m_size] = '\0';
	}

	return *this;
}

String & String::operator+=(const char * str)
{
   
	// TODO: 在此处插入 return 语句
	int slen = strlen(str);
	if (m_size + slen >= m_capacity)
	{
   
		m_capacity = (m_size + slen) % m_cap > 0 ? (m_size 
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值