C++实现串(不引用string)

本文介绍

本文章不使用string.h库来实现串类。
实现了运算符+,=,+=,==,[]的重载。
串类的初始化支持常量字符串,字符串数组,自定义的String类
但是运算符只支持String对象之间(因为懒)

String.h 头文件的实现

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

/*
此String
构造可以由String 或者自带的char*类型字符串构造
但是运算只满足String之间的运算,与内置类型的运算没写,懒
*/

//String 有个注意点,len中没有计算\0
class String {
	friend ostream& operator<<(ostream& cout, String& str);
	friend istream& operator>>(istream& cin, String& str);

public:
	String();//默认构造
	String(char* ch);//构造 面对字符串变量
	String(const char arr[]);//构造 面对常量字符串
	String(const String& str);//构造 面对String类
	~String();//析构函数

	String& operator+(const String& str) const;	//重载+
	String& operator=(const String& str);		//重载=
	String& operator+=(const String& str);		//重载+=  cout的时候要加括号
	bool operator==(const String& str) const;	//重载==
	char& operator[](int index) const;				//重载[]

	int strlen();

	int strstr(String& sub, int pos);//kmp 算法查找子串  从pos下标开始查找子串sub
	void getNext(int* next);

private:
	char* elem;
	int len;
};

String.cpp的实现

#include"String.h"

String::String() {
	this->elem = NULL;
	this->len = 0;
}

String::String(char* ch) {
	//获取长度
	if (!ch) {
		this->len = 0;
		this->elem = NULL;
		return;
	}

	int num = 0;
	while ('\0' != ch[num]) num++;
	this->len = num;

	if (0 != this->len)
	{
		this->elem = new char[num + 1];
		for (int i = 0; i < num + 1; i++) {
			this->elem[i] = ch[i];
		}
	}

	else
	{
		this->elem = new char[1];
		this->elem[0] = '\0';
		this->len = 0;
	}
}

String::String(const char ch[]) {
	//获取长度
	if (!ch) {
		this->elem = NULL;
		this->len = 0;
		return;
	}

	int num = 0;
	while ('\0' != ch[num]) num++;
	this->len = num;

	if (0 != this->len)
	{
		this->elem = new char[num + 1];
		for (int i = 0; i < num + 1; i++) {
			this->elem[i] = ch[i];
		}
	}

	else
	{
		this->elem = new char[1];
		this->elem[0] = '\0';
		this->elem = 0;
	}
}

String::String(const String& str) {
		//获取长度
	if (NULL == str.elem) {
		this->elem = NULL;
		this->len = 0;
		return;
	}

	if (0 != str.len)
	{
		this->len = str.len;
		this->elem = new char[str.len + 1];
		for (int i = 0; i < str.len + 1; i++) {
			this->elem[i] = str.elem[i];
		}
	}

	else
	{
		this->elem = new char[1];
		this->elem[0] = '\0';
		this->elem = 0;
	}
}

String::~String() {
	if (!this->elem) {
		delete[]this->elem;
		this->len = 0;
	}
}
String& String::operator+(const String& str) const {
	char* temp = new char[this->len + str.len + 1];

	for (int i = 0; i < this->len; i++) {
		temp[i] = this->elem[i];
	}

	for (int i = this->len; i < this->len + str.len; i++) {
		temp[i] = str.elem[i - this->len];
	}

	temp[this->len + str.len] = '\0';

	String newString(temp);
	return newString;
}

String& String::operator=(const String& str) {
	//置0
	if (NULL != this->elem) {
		delete[]this->elem;
	}
	this->elem = NULL;
	this->len = 0;

	if (NULL == str.elem) {
		this->elem = NULL;
		this->len = 0;
		return *this;
	}

	if (0 != str.len)
	{

		this->len = str.len;
		this->elem = new char[str.len + 1];
		for (int i = 0; i < str.len + 1; i++) {
			this->elem[i] = str.elem[i];
		}
	}

	else
	{
		this->elem = new char[1];
		this->elem[0] = '\0';
		this->elem = 0;
	}

	return *this;
}

String& String::operator+=(const String& str) {
	char* temp = new char[this->len + str.len + 1];

	for (int i = 0; i < this->len; i++) {
		temp[i] = this->elem[i];
	}

	for (int i = this->len; i < this->len + str.len; i++) {
		temp[i] = str.elem[i - this->len];
	}

	temp[this->len + str.len] = '\0';

	String newString(temp);
	*this = newString;
	return *this;
}

bool String::operator==(const String& str) const {
	//长度不等直接不等
	if (this->len != str.len) return false;

	//长度相等时候的处理
	bool find = true;
	for (int i = 0; i < this->len; i++) {
		if (this->elem[i] != str.elem[i]) {
			find = false;
			break;
		}
	}

	return find;
}

char& String::operator[](int index) const {
	if (index >= this->len || index < 0) {
		cout << "error: index out of range!" << endl;
		exit;
	}

	return this->elem[index];
}

int String::strlen() {
	return this->len;
}

istream& operator>>(istream& cin, String& str) {
	char temp[1000];
	cin >> temp;
	String newString(temp);
	str = newString;
	return cin;
}

ostream& operator<<(ostream& cout, String& str) {
	cout << str.elem;
	return cout;
}

int String::strstr(String& sub, int pos) {
	int lenStr = this->len;
	int lenSub = sub.len;

	if (0 == lenStr || 0 == lenSub)return -1;
	if (pos < 0 || pos >= lenStr)return -1;

	int* next = new int[lenSub];
	sub.getNext(next);

	int i = pos;//遍历主串
	int j = 0;//遍历子串

	while (i < lenStr && j < lenSub)
	{
		if (-1 == j || this->elem[i] == sub[j]) {


			i++;
			j++;
		}
		else {


			j = next[j];
		}
	}

	//释放内存
	delete[] next;

	if (j >= lenSub)return i - j;
	return -1;
}

void String::getNext(int* next) {
	int lenSub = this->len;
	next[0] = -1;
	next[1] = 0;
	int i = 2;//当前i下标
	int k = 0;//前一项k

	while (i < lenSub)
	{
		if (-1 == k || this->elem[i - 1] == this->elem[k]) {
			next[i] = k + 1;
			i++;
			k++;
		}
		else {
			k = next[k];
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的string类是为了更好地管理字符数组而专门设计的。它封装了常用的字符操作函数,使得字符的处理更加方便。在C语言中,关于字符的函数很杂乱无章,与面向对象语言不太相符。而C++string类通过封装这些函数,使得字符的操作更加简洁和高效。\[1\] 关于C++ string类的实现,可以通过模拟实现来理解其原理。在模拟实现中,可以定义一个string类,其中包含了成员变量_size、_capacity和_str,分别表示字符的长度、容量和字符数组。同时,还可以实现一些非成员函数,如重载<<和>>运算符,用于输出和输入字符,以及getline函数,用于读取一行字符。\[2\] 在模拟实现中,可以使用动态内存分配来管理字符的内存空间,通过构造函数和析构函数来初始化和释放内存。同时,还可以实现一些成员函数,如size()函数用于获取字符的长度,capacity()函数用于获取字符的容量,以及其他一些常用的字符操作函数。\[3\] 总之,C++string类通过封装常用的字符操作函数,提供了更加方便和高效的字符处理方式。通过模拟实现可以更好地理解其原理和实现方式。 #### 引用[.reference_title] - *1* *2* [C++string实现](https://blog.csdn.net/qq_53558968/article/details/118428448)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【c++string的底层实现](https://blog.csdn.net/m0_72964546/article/details/127003179)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值