9.24 C++ 常成员,运算符重载

embedded/2024/9/25 1:27:38/
//my_string.cpp
#include "my_string.h"
#include <iostream>
#include <cstring>using namespace std;My_string::My_string():size(15){this->ptr = new char[size];this->ptr[0] = '\0';            //表示串为空串this->len = 0;}//有参构造My_string::My_string(const char* src){this->len=strlen(src);this->size=len+1;this->ptr=new char[size];strcpy(this->ptr,src);}My_string::My_string(int num, char value):size(num+1),len(num){this->ptr=new char[size];memset(this->ptr,value,num);this->ptr[num]='\0';}//拷贝构造My_string::My_string(const My_string &other){this->len=other.len;this->size=other.size;this->ptr=new char[size];strcpy(this->ptr,other.ptr);}//拷贝赋值My_string &My_string::operator=(const My_string &other){if(this!=&other){delete[] this->ptr;this->len=other.len;this->size=other.size;this->ptr=new char[size];strcpy(this->ptr,other.ptr);}return *this;}//析构函数My_string::~My_string(){delete [] this->ptr;}//判空bool My_string::empty() const{return len==0;}//尾插void My_string::push_back(char value){if((len+1)>=size){resize(2*size);}this->ptr[len++]=value;this->ptr[len]='\0';}//尾删void My_string::pop_back(){if(len>0){this->ptr[len-1]='\0';len--;}}//at函数实现char &My_string::at(int index){if(index>=0&&index<len){return this->ptr[index];}}//清空函数void My_string::clear(){this->len=0;this->ptr[0]='\0';}//返回C风格字符串char *My_string::data() const{return this->ptr;}//返回实际长度int My_string::get_length(){return this->len;}//返回当前最大容量int My_string::get_size(){return this->size;}//  +My_string My_string::operator+(const My_string &other)const{My_string temp(this->ptr);temp+=other;return temp;}//[]char &My_string::operator[](int index){return this->ptr[index];}// >bool My_string::operator>(const My_string &other) const{return strcmp(this->ptr,other.ptr)>0;}// <bool My_string::operator<(const My_string &other) const{return strcmp(this->ptr,other.ptr)<0;}// ==bool My_string::operator==(const My_string &other) const{for(int i=0;i<len;i++){if(ptr[i]!=other.ptr[i]){return false;}}return true;}// !=bool My_string::operator!=(const My_string &other) const{return !(*this==other);}// >=bool My_string::operator>=(const My_string &other) const{return !(*this<other);}// <=bool My_string::operator<=(const My_string &other) const{return !(*this>other);}My_string &My_string::operator+=(const My_string &other){for(int i=0;i<other.len;i++){push_back(other.ptr[i]);}return *this;}My_string &My_string::operator+=(char value){push_back(value);return *this;}std::ostream &operator<<(std::ostream &os, const My_string &str){os << str.data();  // 输出字符串内容return os;}std::istream &operator>>(std::istream &is, My_string &str){char temp[1024];is>>temp;str=My_string(temp);return is;}//君子函数:二倍扩容void My_string::resize(int new_size){if (new_size > size) {char *new_ptr = new char[new_size];strcpy(new_ptr, this->ptr);  // 复制旧数据delete[] this->ptr;this->ptr = new_ptr;this->size = new_size;}}
//my_stack.cpp
#include <iostream>class My_stack
{
private:int *data;int maxsize;int top_index;public:My_stack(int max=10):maxsize(max),top_index(-1){data=new int[maxsize];}~My_stack(){delete [] data;}My_stack(const My_stack &other):maxsize(other.maxsize),top_index(other.top_index){data=new int[maxsize];for(int i=0;i<=top_index;i++){data[i]=other.data[i];}}My_stack& operator=(const My_stack & other){delete [] data;maxsize=other.maxsize;top_index=other.top_index;data=new int[maxsize];for(int i=0;i<=top_index;i++){data[i]=other.data[i];}return *this;}bool empty()const{return top_index==-1;}int &top(){return data[top_index];}int size(){return top_index+1;}void push(int value){data[++top_index]=value;}void pop(){--top_index;}
};using namespace std;int main()
{cout << "Hello World!" << endl;return 0;
}


http://www.ppmy.cn/embedded/116371.html

相关文章

基于Vue3组件封装的技巧分享

本文在Vue3的基础上针对一些常见UI组件库组件进行二次封装&#xff0c;旨在追求更好的个性化&#xff0c;更灵活的拓展&#xff0c;提供一些个人的思路见解&#xff0c;如有不妥之处&#xff0c;敬请指出。核心知识点$attrs,$slots 需求 需求背景 日常开发中&#xff0c;我们经…

【鸿蒙HarmonyOS NEXT】用户首选项Preference存储数据

【鸿蒙HarmonyOS NEXT】数据存储之用户首选项Preference 一、环境说明二、Preference运作机制三、示例代码加以说明四、小结 一、环境说明 DevEco Studio 版本&#xff1a; API版本&#xff1a;以12为主 二、Preference运作机制 应用场景&#xff1a; 用户首选项为应用提…

分享两个ADG监控脚本

分享两个监控脚本&#xff0c;用于监控Oracle ADG的状态&#xff0c;如果状态异常则发送邮件告警 脚本一&#xff1a; 利用语句查询日志的应用状态&#xff0c;如果长时间未应用则邮件告警&#xff0c;提醒DBA检查ADG的状态是否异常&#xff1b; 阈值条件&#xff1a;最近一…

Git:Git管理

目录 Git 文件管理检测文件状态 status跟踪新文件 add提交更新 commit撤销提交 Commit Git 校验和历史查看 log版本回退 resetgit 忽略文件 Git 分支管理Git 提交对象Git master分支Git 分支管理本地分支管理远程分支管理分支hotfix处理 Git 工作流常见分支冲突处理分支合并冲突…

QT Creator cmake 自定义项目结构, 编译输出目录指定

1. 目的 将不同的源文件放到不同的目录下进行管理&#xff0c; 如下&#xff1a; build: 编译输出目录 include: 头文件目录 rsources: 资源文件目录 src: cpp文件目录 2. 创建完cmake工程后修改CMakeLists.txt 配置 注 &#xff1a; 这里头文件目录是include, 所以在includ…

python调用c++动态链接库,环境是VS2022和vscode2023

目录 前言&#xff1a;配置环境&#xff1a;基础夯实&#xff08;对于ctypes的介绍&#xff09;&#xff1a;1. 加载共享库2. 定义函数原型3. 调用函数4. 处理数据结构5. 处理指针6. 错误处理7. 使用 ctypes.util总结 效果展示&#xff1a;操作步骤(保姆级教学)一在VS中创建dll…

面试金典题2.2

实现一种算法&#xff0c;找出单向链表中倒数第 k 个节点。返回该节点的值。 输入&#xff1a; 1->2->3->4->5 和 k 2 输出&#xff1a; 4 本题的思路其实很简单&#xff0c;总共n个数&#xff0c;找倒数第k个数&#xff0c;其实就是找第n-k个数。主要的问题是这…

Node-RED和物联网分析:实时数据处理和可视化平台

这篇论文的标题是《Node-RED and IoT Analytics: A Real-Time Data Processing and Visualization Platform》&#xff0c;发表在《Tech-Sphere Journal of Pure and Applied Sciences (TSJPAS)》2024年第一期上。论文主要探讨了Node-RED和物联网分析在物联网(IoT)实时数据处理…