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

EX_25/2/22

找到第一天mystring练习,实现以下功能

mystring str = "hello"

mystring ptr = "world"

str = str + ptr;

str += ptr

str[0] = 'H'

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>using namespace std;class Data {
private:char* p;int len;public:Data();Data(const char* str);Data(const Data& other);~Data();void copy(const Data& str);void append(const Data& str);void show() const;bool compare(const Data& n) const;void swap(Data& n);Data operator+(const Data& other) const;Data& operator+=(const Data& other);char& operator[](int n);Data& operator=(const Data& other);
};Data::Data() {p = NULL;len = 0;
}Data::Data(const char* str) {len = strlen(str);p = (char*)malloc(len + 1);strcpy(p, str);
}Data::Data(const Data& other) {len = other.len;p = (char*)malloc(len + 1);strcpy(p, other.p);
}Data::~Data() {if (p != NULL) { free(p); }
}void Data::copy(const Data& str) {if (p != NULL) { free(p); }len = str.len;p = (char*)malloc(len + 1);strcpy(p, str.p);
}void Data::append(const Data& str) {len = len + str.len;char* backup = p;p = (char*)calloc(1, len + 1);strcpy(p, backup);strcat(p, str.p);free(backup);
}void Data::show() const {cout << p << endl;
}bool Data::compare(const Data& n) const {return strcmp(p, n.p) == 0;
}void Data::swap(Data& n) {char* temp = p;p = n.p;n.p = temp;
}Data Data::operator+(const Data& other) const {Data result;result.len = len + other.len;result.p = (char*)malloc(result.len + 1);strcpy(result.p, p);strcat(result.p, other.p);return result;
}Data& Data::operator+=(const Data& other) {len += other.len;char* backup = p;p = (char*)calloc(1, len + 1);strcpy(p, backup);strcat(p, other.p);free(backup);return *this;
}char& Data::operator[](int n) {return p[n];
}Data& Data::operator=(const Data& other) {if (this == &other) return *this;if (p != NULL) { free(p); }len = other.len;p = (char*)malloc(len + 1);strcpy(p, other.p);return *this;
}int main(int argc, const char** argv) {Data str = "hello";Data ptr = "world";Data combined = str + ptr;combined.show();str += ptr;str.show();str[0] = 'H';str.show();if (str.compare(ptr)) {cout << "str 和 ptr 一样" << endl;} else {cout << "str 和 ptr 不一样" << endl;}str.swap(ptr);str.show();ptr.show();return 0;
}

封装消息队列

class Msg{

key_t key

int id;

int channel }

实现以下功能

Msg m("文件名")

m[1].send("数据"),

将数据发送到1号频道中 string str = m[1].read(int size) 从1号频道中读取消息,并且返回

#include <sys/types.h>
#include <iostream>
#include <string>
#include <cstring>
#include <sys/ipc.h>
#include <sys/msg.h>using namespace std;class Msg {
public:Msg(const string& filename) {key = ftok(filename.data(), 'A');id = msgget(key, 0666 | IPC_CREAT);}void send(int n, const string& text) {msgbuf buf{n, {}};strncpy(buf.text, text.data(), sizeof(buf.text));msgsnd(id, &buf, strlen(buf.text) + 1, 0);}string read(int n) {msgbuf buf{};msgrcv(id, &buf, sizeof(buf.text), n, 0);return string(buf.text);}private:key_t key;int id;struct msgbuf {long n;char text[1024];};
};int main() {Msg m("msgfile");m.send(1, "hello, channel 1!");string msg = m.read(1);cout << msg << endl;return 0;
}

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

相关文章:

  • rust安装教程以及git连接到远程仓库
  • Kafka系列之:记录一次源头数据库刷数据,造成数据丢失的原因
  • VC++零基础入门之系列教程 【附录E MFC快速参考指南】
  • 在CentOS 7下部署NFS的详细教程
  • LabVIEW C编译支持工具库CCompileSupp.llb
  • 【含文档+PPT+源码】基于微信小程序的农产品自主供销商城系统
  • MongoDB私人学习笔记
  • C++---了解STL
  • 智能自动化新纪元:AI与UiPath RPA的协同应用场景与技术实践
  • 2025年2月科技热点深度解析:AI竞赛、量子突破与开源革命
  • 计算机网络————(三)
  • 请谈谈 React 中的状态管理,如何使用 Context API 和 Redux 进行状态管理?
  • 【考研】复试相关上机题目
  • 利用机器学习实现实时交易欺诈检测
  • Modelfile配置说明
  • labview实现有符号位16进制转二进制补码转真值
  • 浏览器深度解析:打造极速、安全、个性化的上网新体验
  • JavaScript 简单类型与复杂类型-堆和栈
  • 【AI+智造】DeepSeek价值重构:当采购与物控遇上数字化转型的化学反应
  • 基于YOLO11深度学习的苹果叶片病害检测识别系统【python源码+Pyqt5界面+数据集+训练代码】
  • mapbox添加自定义图片绑定点击事件,弹窗为自定义组件
  • SVT-AV1接入ffmpeg说明
  • 基于 C++ Qt 的 Fluent Design 组件库 QFluentWidgets
  • OpenCV(6):图像边缘检测
  • 多模态人物视频驱动技术回顾与业务应用
  • 星海智算+ DeepSeek-R1:技术突破与行业应用的协同革新
  • 选择排序:简单高效的选择
  • 考研/保研复试英语问答题库(华工建院)
  • ARM Cortex-M处理器中的MSP和PSP
  • 《Keras 3 使用 NeRF 进行 3D 体积渲染》:此文为AI自动翻译