【殷人昆数据结构】第四章4.1 字符串数组AString代码的调试

这篇博客详细介绍了AString字符串数组的实现,包括主函数、类定义、构造函数、substr函数、各种运算符重载(如+,+=,[]),以及find和replace等函数的实现原理。在讨论运算符重载时,解释了为何需要使用临时变量temp来避免内存泄漏,并分析了find函数和fastfind函数的查找策略。
摘要由CSDN通过智能技术生成

AString字符串数组

又是字符串数组。

主函数

#include "AString.h"
#include <iostream>
using namespace std;

int main(){
   
	cout << "Please input a string : ";
	AString str;
	cin >> str;
	cout << "The length of the string is: " << str.Length() << endl;

	cout << "\n1. Test the substr(int pos, int len): \n";
	int pos, len;
	cout << "Input the pos and the len: ";
	cin >> pos >> len;
	AString sub = str.substr(pos, len);
	cout << "The substring is: " << sub << endl;

	cout << "\n2. Test the operator()(int pos, int len): \n";
	cout << "Input the pos and the len: ";
	cin >> pos >> len;
	sub = str(pos, len);
	cout << "The substring is: " << sub << endl;
	
	cout << "\n3. Test the operator[](int i): \n";
	cout << "Input the positon i:";
	int i;
	cin >> i;
	cout << "The char in position " << i << " is: " << str[i] << endl;

	AString str2;
	cout << "Input a new string:\n";
	cin >> str2;

	cout << "\n4. Test the operator ==\n";
	cout << "The two strings " << "\"" << str << "\" and \"" << str2 << "\"";
	if (str == str2)  cout << " is Equal!\n";
	else	cout << " is not Equal!\n";

	cout << "\n5. Test the operator !=\n";
	cout << "The two strings " << "\"" << str << "\" and \"" << str2 << "\"";
	if (str != str2)	cout << " is not Equal!\n";
	else	cout << " is Equal!\n";
	
	cout << "\n6. Test the operator +\n";
	AString tmp = str + str2;
	cout << "\"" << str << "\" + \"" << str2 
		<< "\" = \"" << tmp << "\"\n";
	
	cout << "\n7. Test the operator +=\n";
	cout << "\"" << str << "\" += \"" << str2 
		<< "\", then the result is: \n";
	str += str2;
	cout << str << endl;

	cout << "\n8. Test int fastFind(AString& pat, int k, int *next):\n";
	cout << "Input a string and a substring: " << endl;
	cin >> str >> str2;
	int *next = new int[str2.Length()+1];//ûÓÐ+1ʹÓÃÖгö½ç£¬Îö¹¹Ê±ÓÐÎÊÌâ
	str2.getNext(next);
	int f = str.fastFind(str2, 0, next);
	delete [] next;
	if(f == -1)	cout << "Not Find!" << endl;
	else	cout << "Find,the position is : " << f << endl;
	
	cout << "\n9. Test the replace(AString &t, AString &v):\n";
	AString s("aabbabcbaabaaacbab");
	AString t("bab");
	AString v("abdc");
	cout << "The initial string is: " << s << endl;
	cout << "The string t is: " << t << endl;
	cout << "The string v is: " << v << endl;
	s.replace(t, v);
	cout << "After s.replace(t, v), the string is: " << s << endl;

	return 0;
}

AString类定义

const int defaultSize=128;
class AString{
   
private:
	char *ch;
	int curLength;
	int maxSize;
public:
	AString(int sz = defaultSize);
	AString(const char *init );
	AString(const AString &ob);
	~AString(){
   
		delete []ch;
	}
	int Length()const{
   
		return curLength;
	}
	AString substr(int pos, int len);
	AString operator()(int pos, int len);	//取子串
	bool operator == (AString &ob)const	{
   
		return strcmp(ch, ob.ch) == 0;
	}
	bool operator != (AString &ob)const{
   
		return strcmp (ch, ob.ch) != 0;
	}
	bool operator !()const{
                   //判断串是否为空
		return curLength == 0;
	} 
	AString& operator = (const AString &ob);
	AString& operator += (const AString &ob);
	AString operator + (const AString &ob);
	char& operator <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值