基本信息
源码名称:自写String类即使用
源码大小:7.64KB
文件格式:.cpp
开发语言:C/C++
更新时间:2020-03-31
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 3 元×
微信扫码支付:3 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
自己动手写一个String实例,可实现基本的读写,查找,比较等等功能
#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;
}