当前位置: 首页 > news >正文

C++day1

 

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>using namespace std;class mystring {
private:char* buf;int len;
public:// 单参构造函数,支持隐式调用,列表初始化 lenmystring(const char* str): len(strlen(str)){buf = new char[len + 1];strcpy(buf, str);}// copy 函数,从 mystring 对象拷贝void copy(const mystring& other) {len = other.len;buf = new char[len + 1];strcpy(buf, other.buf);}// copy 函数,从 C 风格字符串拷贝void copy(const char* str) {len = strlen(str); buf = new char[len + 1];strcpy(buf, str);}// append 函数,追加 mystring 对象内容void append(const mystring& other) {int newLen = len + other.len;char* newBuf = new char[newLen + 1];strcpy(newBuf, buf);strcat(newBuf, other.buf);buf = newBuf;len = newLen;}// append 函数,追加 C 风格字符串内容void append(const char* str) {int strLen = strlen(str);int newLen = len + strLen;char* newBuf = new char[newLen + 1];strcpy(newBuf, buf);strcat(newBuf, str);buf = newBuf;len = newLen;}// compare 函数,比较 mystring 对象int compare(const mystring& other) {return strcmp(buf, other.buf);}// compare 函数,比较 C 风格字符串int compare(const char* str)  {return strcmp(buf, str);}// show 函数,输出字符串void show()  {cout << buf << endl;}// at 函数,获取指定位置字符char at(int index)  {if (index >= 0 && index < len) {return buf[index];}// 这里可根据需求处理越界,简单返回空字符或抛异常等,这里返回空字符示例return '\0';}
};int main() {mystring str = "hello";mystring ptr = "world";str.copy(ptr);str.copy("你好");str.append(ptr);str.append("你好");str.compare(ptr);str.compare("你好");str.show();cout << str.at(0) << endl;return 0;
}

http://www.lryc.cn/news/597351.html

相关文章:

  • Android网络请求,Retrofit,OKHttp学习
  • Python爬虫--Xpath的应用
  • rust嵌入式开发零基础入门教程(五)
  • 使用阿里云 ESA 边缘函数转发代理 docker registry
  • OpenLayers 快速入门(十)常用 API 补充
  • 量化金融简介(附电子书资料)
  • 分布式系统中的缓存设计与应用
  • 三步实现Android系统级集成:预装Google TTS + 默认引擎设置 + 语音包预缓存方案
  • AI助力,轻松实现人声分离伴奏提取
  • 【金融机器学习】第五章:最优投资组合——Bryan Kelly, 修大成(中文翻译)
  • 手机开启16k Page Size
  • 大模型【进阶】(四)QWen模型架构的解读
  • SpringBoot07-数据层的解决方案:SQL
  • FireFox一些设置
  • latex中既控制列内容位置又控制列宽,使用>{\centering\arraybackslash}p{0.85cm}
  • OpenLayers 快速入门(二)Layer 对象
  • 深入掌握 Python 面向对象的灵魂——魔法函数(Magic / Dunder Methods)全景指南
  • CAN的终端电阻
  • 设计模式代码总结
  • 用 PyTorch 实现全连接网络识别 MNIST 手写数字
  • Android插件化实现方案深度分析
  • window下c++共享内存,进程互斥锁。
  • macOS配置maven及报错处理:zsh: permission denied: mvn
  • 大厂总结常用分析问题方法之CMMI-IDEAL模型
  • VRRP技术-设备备份技术
  • Modbus TCP转Devicenet:水泥厂PLC与多类仪表的自动化通信实践
  • 学习 Flutter(五):玩安卓项目实战 - 下
  • 2025年7月一区SCI-投影迭代优化算法Projection Iterative Methods-附Matlab免费代码
  • Flutter学习笔记(四)---基础Widget
  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘jupyter’问题