首页 > 编程语言 >2024.9.3C++

2024.9.3C++

时间:2024-09-04 19:51:29浏览次数:5  
标签:return 2024.9 len other C++ str mystring const

自行实现Mystring类

#include <iostream>
#include <cstring>
using namespace std;

class mystring
{
public:
    mystring()
    {
        len = 0;
        str = nullptr;
    }
    mystring(const char* s)
    {
        len = strlen(s);
        str = new char[len + 1];
        strcpy(str, s);
    }
    mystring(const mystring& other)
    {
        len = other.len;
        str = new char[len + 1];
        strcpy(str, other.str);
    }
    ~mystring()
    {
        delete[] str;
    }
    mystring& operator=(const mystring& other)
    {
        if (this != &other)
        {
            len = other.len;
            delete[] str;
            str = new char[len + 1];
            strcpy(str, other.str);
        }
        return *this;
    }
    mystring operator+(const mystring& other)
    {
        mystring s;
        s.len = len + other.len;
        s.str = new char[s.len + 1];
        strcpy(s.str, str);
        strcat(s.str, other.str);
        return s;
    }

    bool operator!=(const mystring& other)
    {
        if(strcmp(str, other.str)!= 0)
        {
            return true;
        }
        return false;

    }
    bool operator==(const mystring& other)
    {
        if(strcmp(str, other.str) == 0)
        {
            return true;
        }
        return false;
    }
    bool operator<(const mystring& other)
    {
        if(strcmp(str, other.str) < 0)
        {
            return true;
        }
        return false;
    }
    bool operator>(const mystring& other)
    {
        if(strcmp(str, other.str) > 0)
        {
            return true;
        }
        return false;
    }
    bool operator<=(const mystring& other)
    {
        if(strcmp(str, other.str) <= 0)
        {
            return true;
        }
        return false;
    }
    bool operator>=(const mystring& other)
    {
        if(strcmp(str, other.str) >= 0)
        {
            return true;
        }
        return false;
    }

    const char* data() const
    {
        return str;
    }
    bool empty() const
    {
        return len == 0;
    }
    int size() const
    {
        return len;
    }
    int length() const
    {
        return len;
    }
    char at(int index) const
    {
        if (index < 0 || index >= len)
        {
            throw out_of_range("Index out of range");
        }
        return str[index];
    }
    friend ostream &operator<<(ostream& L, const mystring &R);
    friend istream &operator>>(istream& L, mystring &R);
private:
    char* str;
    int len;
};
ostream& operator<<(ostream& L, const mystring &R)
{
    L << R.str<<endl;
    return L;
}

istream& operator>>(istream& L,mystring &R)
{
    L >> R.str;
    return L;
}

int main()
{
    mystring s1("hello");
    mystring s2(s1);
    cout << s1.data() << endl;
    cout << s2.data() << endl;
    s2 = mystring("world");
    cout << s1.data() << endl;
    cout << s2.data() << endl;
    cout << s1.size() << endl;
    cout << s2.size() << endl;
    cout << s1.empty() << endl;
    cout << s2.empty() << endl;
    cout << s1.length() << endl;
    cout << s2.length() << endl;
    try {
        cout << s1.at(0) << endl;
        cout << s2.at(0) << endl;
    } catch (const out_of_range& e) {
        cerr << e.what() << endl;
    }
    s1 = s1 + s2;
    cout << s1.data() << endl;
    cout << s2.data() << endl;
    cout << (s1 == s2) << endl;
    cout << (s1 != s2) << endl;
    cout << (s1 < s2) << endl;
    cout << (s1 > s2) << endl;
    cout << (s1 <= s2) << endl;
    cout << (s1 >= s2) << endl; 
    cout << s1 << endl;
    cin >> s1;
    cout << s1 << endl;

    return 0;
}

标签:return,2024.9,len,other,C++,str,mystring,const
From: https://blog.csdn.net/qq_63490254/article/details/141869633

相关文章

  • 2024.9.2C++作业
    自行实现一个Mystring类#include<iostream>#include<cstring>usingnamespacestd;classmystring{public:mystring(){len=0;str=nullptr;}mystring(constchar*s){len=strlen(s);str=n......
  • C++基础之杂项
    目录思维导图:学习内容:1. Lambda表达式1.1基本概念1.2定义格式1.3常用情况二、异常处理2.1什么是异常处理2.2何时使用异常处理2.3异常处理的格式2.4异常实例2.5构造和析构中的异常 2.6系统提供异常类 三、C++中文件操作3.1文件流对象的介绍3.2关......
  • Codeforces Round 971 (Div. 4) ABCD题详细题解(C++,Python)
    前言:    本文为CodeforcesRound971(Div.4)ABCD题的题解,包含C++,Python语言描述,觉得有帮助或者写的不错可以点个赞    比赛打了没一半突然unrated了就不是很想继续写了,早起写个题解    (之前的div3也没复盘,哎真菜)目录题A:题目大意和解题......
  • AtCoder Beginner Contest 369 题ABCD详细题解--包含解题思路和两种语言(C++,Python)
    前言:    本文为AtCoderBeginnerContest369题ABCD详细题解,包括题目大意,详细的解题思路和两种语言描述,觉的有帮助或者写的不错可以点个赞几天前的比赛拖的有点久了比赛题目连接:Tasks-AtCoderBeginnerContest369目录题A:题目大意和解题思路:代码(C++):......
  • c++学习7、函数探幽
    #include<iostream>#include<string>usingnamespacestd;structcrdit{ stringname; intage; stringaddress;};structjob{ charname[40]; doublesalary; intfloor;};inlinedoublesquare(doublex){returnx*x;}//定义了一个内联函数square—......
  • C++和OpenGL实现3D游戏编程【连载7】——文字和汉字的显示
    1、本节实现的内容上一节我们讨论了纹理在二维平面内不规则图形贴图的相关基础操作,本节我们开始了解游戏里文字以及汉字的显示方法。本节课我们将从基本的ASCII字符显示,拓展到中文字符的显示,最后再讲到纹理字符的显示,并对各种文字显示方法的优缺点和使用场景进行分析,这节课......
  • 初次部分使用c++语言无意发现个数组元素查找的代码
    includeusingnamespacestd;//定义结构体structMyArray{intarr[100];//数组,假设最大长度为100intsize;//数组当前元素数量};//输入函数,修改数组内容voidscanf(MyArray&myArray){for(inti=0;i<myArray.size;i++){cin>>myArray.arr[i];//键盘......
  • 南沙信奥赛C++陈老师解一本通题:1341:【例题】一笔画问题
    ​ 题目描述】如果一个图存在一笔画,则一笔画的路径叫做欧拉路,如果最后又回到起点,那这个路径叫做欧拉回路。根据一笔画的两个定理,如果寻找欧拉回路,对任意一个点执行深度优先遍历;找欧拉路,则对一个奇点执行dfs,时间复杂度为O(m+n),m为边数,n是点数。【输入】第一行n,m,有n个点,m条......
  • 【C++从练气到飞升】19---哈希:哈希冲突 | 哈希函数 | 闭散列 | 开散列
     ......
  • C++:this指针详解
    目录一、this指针 二、C++和C语言实现Stack对比一、this指针•Date类中有Init与Print两个成员函数,函数体中没有关于不同对象的区分,那当d1调用Init和Print函数时,该函数是如何知道应该访问的是d1对象还是d2对象呢?那么这里就要看到C++给了一个隐含的this指针解决这里......