C++自定义String类(简单的实现功能)

#pragma once#include "targetver.h"#include#include #include #includeusing namespace std;#pragma onceclass MyString{public:MyString(char *s);MyString();MyString(MyString &
摘要由CSDN通过智能技术生成


#pragma once


#include "targetver.h"
#include<cassert>
#include <stdio.h>
#include <tchar.h>
#include<iostream>

using namespace std;

#pragma once
class MyString
{
public:
MyString(char *s);
MyString();
MyString(MyString & a);
char* mystrcopy(char *str1, const char * str2);//自定义strcpy函数
MyString &mystrcopy(MyString& a, const MyString &b);//自定义strcpy函数形参为两个对象
MyString &mystrcopy( const char * str2);//字符指针copy到自身
MyString &mystrcopy(const MyString &a);//另一个对象copy到自身
unsigned int mystrlen(const char * str);//自定义strlen函数
unsigned int mystrlen()const;//直接计算自身长度
unsigned int mystrlen(const MyString &a);//计算另一个对象的长度
char * mystrcat(char* str1, const char* str2);//自定义strcat函数
MyString &mystrcat(MyString &a, const MyString &b);//链接两个对象
MyString &mystrcat(const char* str2);//将另一字符串链接自身
MyString & mystrcat(const MyString &a);//将另一个对象链接至自身
int Mystrcmp(const char * str1,const char * str2);//自定义比较函数
int Mystrcmp(const MyString& a, const MyString& b);//比较两个对象
int Mystrcmp( const char * str2);//比较自身和一个字符串
int Mystrcmp(const MyString &a);//比较自身和一个对象
void GetNext(int Next[], const char * str);//跳转数组
int FindStr(const char *str1,const char *str2);//查询参数字符串在自身的位置,返回位置
void GetNext(int Next[], const MyString &b);//跳转数组参数为对象的引用
int FindStr(const MyString &a,const MyString &b);//查询参数字符串在自身的位置,返回位置
int FindStr(const MyString &b);//查询参数字符串在自身的位置,返回位置
int myatoi(char *str);//字符串转int
int myatoi(const MyString &a);//字符串转int
int myatoi();//字符串转int
char *my_itoa( int num, char *str, int radix);//int转字符串
MyString &my_itoa(int num, MyString & a, int radix);//int转字符串
MyString &my_itoa(int num,  int radix);//int转字符串
char* antitone(char *str);//反序
MyString antitone(MyString &a);//反序
MyString & antitone();//反序
char* insert(int n, char *str, char *c);//插入
MyString & insert(int n, MyString& a, char *c);//插入对象为参数
MyString & insert(int n,  char *c);//插入自身为参数
MyString & insert(int n, MyString& a, const MyString& b);//插入对象插对象
MyString & insert(int n, const MyString& b);//插入
char *ReMove(unsigned int m, char*str, unsigned int n);//删除
MyString &ReMove(unsigned int m, MyString&a, unsigned int n);//从对象中删除
MyString &ReMove(unsigned int m, unsigned int n);//从自身删除
char* &GETS(){ return s; }
friend ostream &operator<<(ostream &a, MyString & b);//重载输出流
friend istream &operator>>(istream &a, MyString & b);//重载输入流
friend MyString&operator+=(MyString &a, const MyString & b);//重载+=实现字符串链接
friend MyString operator+(MyString &a, const MyString & b);//重载+实现字符串链接但不改变对象
friend MyString&operator-=(MyString &a, const MyString & b);//重载-=实现字符串删除                               当a中没有b是不变
friend MyString operator-(MyString &a, const MyString & b);//重载-实现字符串删除但不改变对象                     当a中没有b是不变
MyString&operator=( const MyString & b);//由于用到堆区赋值运算符需要重写
static char* pp;
static char* hh;
static int aa;
static bool rr;
virtual ~MyString();
protected:
private:
char * s;
};
#include "stdafx.h"
#include "MyString.h"
char *MyString::hh = NULL;
char* MyString::pp = NULL;
int MyString::aa = 0;
bool MyString::rr = true;
char *MyString::mystrcopy(char *str1, const char *  str2)//自定义strcpy函数
{
hh = str1;
assert(hh != NULL&&str2 != NULL);
while ((*hh++ = *str2++) != '\0'){}
hh = NULL;
return str1;
}
MyString &MyString::mystrcopy(MyString& a, const MyString& b)//自定义strcpy函数形参为两个对象
{
hh = a.s;
pp= b.s;
assert(hh != NULL&&pp != NULL);
while ((*hh++ = *pp++) != '\0'){}
hh = NULL;
pp = NULL;
return a;
}
MyString & MyString::mystrcopy(const MyString &a)//自定义strcpy函数形参为一个对象
{
pp = this->s;
hh = a.s;
assert(pp != NULL&&hh != NULL);
while ((*pp++ = *hh++) != '\0'){}
hh = NULL;
pp = NULL;
return *this;
}
MyString & MyString::mystrcopy(const char * str2)//自定义strcpy函数形参为一个字符串
{
pp = this->s;
assert(pp != NULL&&str2 != NULL);
while ((*pp++ = *str2++) != '\0'){}
pp = NULL;
return *this;
}
unsigned int MyString::mystrlen(const char * str)//自定义strlen函数
{
assert(str != NULL);
aa = 0;
for (; *str++ != '\0'; aa++){}
return aa;
}
unsigned int MyString::mystrlen()const//自定义strlen函数另一无形参为自身
{
pp = s;
assert(pp != NULL);
aa = 0;
for (; *pp++ != '\0'; aa++){}
pp = NULL;
return aa;
}
unsigned int MyString::mystrlen(const MyString &a)//自定义strlen函数另一参数为形参为一个对象
{
pp = a.s;
assert(pp != NULL);
aa= 0;
for (; *pp++ != '\0'; aa++){}
<

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,我们可以通过继承`std::exception`自定义异常。`std::exception`是C++标准库中的一个基,它包含了一个`what()`成员函数,用于返回异常的描述信息,因此我们可以通过继承`std::exception`实现自定义异常,并在其中重载`what()`函数来返回异常信息。 下面是一个自定义异常的示例代码: ``` #include <exception> #include <string> class MyException : public std::exception { public: MyException(const std::string& msg) : m_msg(msg) {} virtual const char* what() const noexcept { return m_msg.c_str(); } private: std::string m_msg; }; ``` 在上面的代码中,我们定义了一个名为`MyException`的自定义异常,它继承自`std::exception`。在`MyException`的构造函数中,我们传入一个`std::string`型的参数`msg`,用于保存异常信息。在`MyException`中,我们重载了`what()`函数,用于返回异常信息。在`what()`函数中,我们通过`c_str()`函数将`m_msg`字符串转换为C风格的字符串,并返回。 使用自定义异常的示例代码: ``` #include <iostream> void foo() { throw MyException("Something bad happened."); } int main() { try { foo(); } catch (std::exception& e) { std::cout << "Caught exception: " << e.what() << std::endl; } return 0; } ``` 在上面的代码中,我们定义了一个名为`foo()`的函数,其中抛出了一个`MyException`型的异常。在`main()`函数中,我们使用`try-catch`语句块捕获异常,并在`catch`语句块中输出异常信息。运行程序,输出结果如下: ``` Caught exception: Something bad happened. ``` 可以看到,我们成功地捕获了自定义异常,并输出了异常信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值