在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例常规C/C++编程 → 自写String类即使用

自写String类即使用

常规C/C++编程

下载此实例
  • 开发语言:C/C++
  • 实例大小:7.64KB
  • 下载次数:8
  • 浏览次数:106
  • 发布时间:2020-03-31
  • 实例类别:常规C/C++编程
  • 发 布 人:学海无涯苦作舟
  • 文件格式:.cpp
  • 所需积分:3
 相关标签:

实例介绍

【实例简介】

自己动手写一个String实例,可实现基本的读写,查找,比较等等功能

【实例截图】from clipboard
【核心代码】
#define _CRT_SECURE_NO_WARNINGS
#define _CRTDBG_MAP_ALLOC

#include <iostream>
#include<istream>
#include<ostream>
#include <crtdbg.h>  //用于检测内存泄漏

#ifdef _DEBUG
	#ifndef DBG_NEW
		#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
	#define new DBG_NEW
	#endif
#endif  // _DEBUG

using namespace std;
class MyString
{
public:
	//默认无参构造
	MyString();
	//构造函数
	MyString(int len);
	MyString(const char* s);
	MyString(char c, int n);
	MyString(const MyString& s);
	//重载>>运算符,只能在类外实现
	friend ostream& operator<<(ostream& os, MyString& s);
	//重载<<运算符,只能在类外实现
	friend  istream& operator>>(istream& is, MyString& s);
	//重载==运算符,类外实现
	friend bool operator==(const MyString& s1, const MyString& s2);
	//重载()运算符
	MyString& operator()(const MyString& str);
	//重载=运算符
	MyString operator=(const MyString& str);
	//重载 运算符
	MyString operator (const MyString& str);
	//重载[]运算符
	char& operator[](int index);
	//重载<运算符
	bool operator<(const MyString& str2);
	//重载<=运算符
	bool operator<=(const MyString& str2);
	//重载>运算符
	bool operator>(const MyString& str2);
	//重载>=运算符
	bool operator>=(const MyString& str2);
	//字符寻址函数
	int FindChar(char s);
	//长度检测函数
	int length()const;
	//字符串插入函数
	void insert(int p0, const char* s);
	//子串截取函数
	MyString substr(int pos, int n) const;
	//字符串交换函数
	void swap(MyString& s);
	//析构函数
	~MyString();
private:
	//长度
	int len;
	//S指针
	char* str;
};
//默认构造函数
MyString::MyString()
{
	this->len = 0;
	this->str = NULL;
}
//构造函数
MyString::MyString(int len)
{
	//如果传入的len不合理
	if (len <= 0)
	{
		this->len = 0;
		this->str = NULL;
		return;
	}
	//如果传入的len合理
	else
	{
		this->len = len;
		this->str = new char[this->len   1];
	}
}
//构造函数
MyString::MyString(const char* s)
{
	if (s == NULL)
	{
		this->len = 0;
		this->str = new char[0   1];
		strcpy(this->str, "");
	}
	else
	{
		this->len = strlen(s);
		this->str = new char[this->len   1];
		strcpy(this->str, s);
	}
}

//构造函数
MyString::MyString(char c, int n)
{
	this->len = n;
	this->str = new char[n   1];
	int i;
	for (i = 0; i < n;   i) {
		*(this->str   i) = c;
	}
	*(this->str   i) = '\0';
}
//构造函数
MyString::MyString(const MyString& s)
{
	this->len = s.len;
	this->str = new char[this->len   1];
	strcpy(this->str, s.str);
}

//重载==运算符,类外实现,重载为友元函数
bool operator==(const MyString& s1, const MyString& s2)
{
	if (s1.len != s2.len) return false;
	else {
		for (int i = 0; i < s1.len;   i) {
			if (*(s1.str   i) == *(s2.str   i)) continue;
			else {
				return false;
				break;
			}
		}
		return true;
	}
}

//重写()运算符,返回的是MyString &
MyString& MyString::operator()(const MyString& s)
{
	if (this->str != NULL)
	{
		this->len = 0;
		delete this->str;
		this->str = NULL;
	}
	this->len = s.len;
	this->str = new char[this->len   1];
	strcpy(this->str, s.str);
	return *this;
}

//重写=运算符返回的是MyString &
MyString  MyString::operator=(const MyString& s)
{
	if (this == &s)
	{
		return *this;
	}
	if (this->str != NULL)
	{
		this->len = 0;
		delete this->str;
		this->str = NULL;
	}
	this->len = s.len;
	this->str = new char[this->len   1];
	strcpy(this->str, s.str);
	return *this;
}

//重写 运算符,返回的是匿名对象
MyString  MyString::operator (const MyString& s)
{
	int Len = this->len   s.len;
	MyString temp(Len);
	strcpy(temp.str, this->str);
	strcat(temp.str, s.str);
	return temp;
}

//重写[]运算符,返回的是char的引用
char& MyString::operator[](int index)
{
	if (index > this->len - 1)
	{
		return this->str[this->len - 1];  //如果超出引索范围,则默认返回最后一个字符
	}
	return this->str[index];
}

//重载<运算符
bool MyString::operator<(const MyString& str2)
{
	int  i = 0;
	i = strcmp(this->str, str2.str );
	if (i >= 0) return false;
	else return true;
}

//重载<=运算符
bool MyString::operator<=(const MyString& str2)
{
	int  i = 0;
	i = strcmp(this->str, str2.str);
	if (i > 0) return false;
	else return true;
}

//重载>运算符
bool MyString::operator>(const MyString& str2)
{
	int  i = 0;
	i = strcmp(this->str, str2.str);
	if (i <= 0) return false;
	else return true;
}

//重载>=运算符
bool MyString::operator>=(const MyString& str2)
{
	int  i = 0;
	i = strcmp(this->str, str2.str);
	if (i < 0) return false;
	else return true;
}

//字符寻址函数,若能找到字符,则返回引索值,否则返回-1
int MyString::FindChar(char s)
{
	for (int i = 0; i < len;   i) {
		if (*(str   i) == s) {
			return i;
		}
	}
	return EOF;
}

//析构函数
MyString::~MyString()
{
	if (this->str != NULL)
	{
		delete str;
		str = NULL;
		this->len = 0;

	}
}
//重写<<运算符,返回的是ostream &
ostream& operator<<(ostream& os, MyString& s)
{
	//输出字符串
	os << s.str;
	return os;
}
//重写<<运算符,返回的是istream &
istream& operator>>(istream& is, MyString& s)
{
	if (s.str != NULL)
	{
		delete s.str;
		s.str = NULL;
		s.len = 0;
	}
	char temp[4096] = { 0 };
	is >> temp;
	int len = strlen(temp);
	s.str = new char[len   1];
	strcpy(s.str, temp);
	s.len = len;
	return is;
}
//长度检测函数
int MyString::length() const
{
	return this->len;
}
//字符串插入函数
void MyString::insert(int p0, const char* s)
{
	char* temp;
	temp = new char[this->len   strlen(s)   1];
	int i = 0;
	for (i = 0; i < p0;   i)
	{
		*(temp   i) = *(this->str   i);
	}
	for (i = p0; i < p0   strlen(s);   i)
	{
		*(temp   i) = *(s   i - p0);
	}
	for (i = p0   strlen(s); i < this->len   strlen(s)   1;   i)
	{
		*(temp   i) = *(this->str   i - strlen(s));
	}
	delete[] this->str;
	this->str = temp;
}
//子串截取函数
MyString MyString::substr(int pos, int n) const
{
	char* temp;
	temp = new char[n   1];
	for (int i = pos; i < pos   n;   i)
	{
		*(temp   i - pos) = *(this->str   i);
	}
	*(temp   n) = '\0';
	MyString t(temp); //创建一个临时的MyString对象,然后释放temp的空间,防止内存泄漏
	delete[] temp;
	return t;
}
//字符串交换函数
void MyString::swap(MyString& s)
{
	char* temp;
	temp = this->str;
	this->str = s.str;
	s.str = temp;
}
int main()
{
	//检测内存情况,若出现异常,则输出详细的内存泄漏报告
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

	//测试MyString的基本构造功能
	MyString s1("123");
	cout << "s1:" << s1 << endl;
	MyString s2("abc");
	cout << "s2:" << s2 << endl;
	MyString s3(s1);
	cout << "s3:" << s3 << endl;
	MyString s4 = s2;
	cout << "s4:" << s4 << endl;
	MyString s5 = s1   s2   s3;
	cout << "s5:" << s5 << endl;
	s5[1] = 'a';   //测试单个字符替换的功能
	cout << "s5:" << s5 << endl;
	MyString s6('s', 5);
	cout << s6 << endl << endl << endl;

	//测试重载I/O流的功能
	cout << "输入新的s1和s5,中间以空格隔开,例如:789 abc" << endl;
	cin >> s1 >> s5;
	cout << "s1:" << s1 << endl;
	cout << "s5:" << s5 << endl;
	cout << "s1与s5相等吗? "<<(s1 == s5) << endl << endl << endl;  //调用重载后的==运算符,比较二者

	//测试插入函数
	MyString s7 = "abc";
	char test[5] = { '1','2'};
	s7.insert(2, test);
	cout << "s7: "<< s7 << endl << endl << endl;

	//测试字符串截取函数
	MyString s8 = "abcdefghijklmn";
	MyString s9 = s1.substr(1, 3);
	cout << "从s8截取后得到的s9为: " << s9 << endl << endl << endl;

	//测试字符串交换函数
	s8.swap(s9);
	cout << "交换s8,s9后" << s9 << endl << endl << endl;

	//测试引索功能
	cout << "输入一个引索,找出 s1 对应位置的字符:" << endl;
	int i;
	cin >> i;
	cout << "Char at s1 " << i << " is : " << s1[i] << endl;
	char tests;
	cout << "输入一个字符,查找之:" << endl;
	cin >> tests;
	cout << tests << " at s1 " << s1.FindChar(tests) << endl << endl << endl;

	//字符串比较运算符重载测试
	MyString s10("abc"), s11("abd");
	cout << "s10 < s11 ? " << (s10 < s11) << endl;
	cout << "s10 <= s11 ? " << (s10 <= s11) << endl;
	cout << "s10 > s11 ? " << (s10 > s11) << endl;
	cout << "s10 >= s11 ? " << (s10 >= s11) << endl << endl << endl;


	//暂停,显示结果
	cout << "测试完成!!!" << endl;
	system("pause");
	return 0;

}

标签:

实例下载地址

自写String类即使用

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警