EX_25/2/22

ops/2025/2/27 16:57:10/

找到第一天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.ppmy.cn/ops/161726.html

相关文章

React + TypeScript 复杂布局开发实战

React TypeScript 复杂布局开发实战 一、项目架构设计&#xff08;基于最新技术栈&#xff09; 1.1 技术选型与工程创建 # 使用Vite 5.x React 19 TypeScript 5.4 npx create-vitelatest power-designer-ui --template react-ts cd power-designer-ui && npm inst…

个人电脑小参数GPT预训练、SFT、RLHF、蒸馏、CoT、Lora过程实践——MiniMind图文版教程

最近看到Github上开源了一个小模型的repo&#xff0c;是真正拉低LLM的学习门槛&#xff0c;让每个人都能从理解每一行代码&#xff0c; 从零开始亲手训练一个极小的语言模型。开源地址&#xff1a; GitHub - jingyaogong/minimind: &#x1f680;&#x1f680; 「大模型」2小时…

vue2项目打包后js文件过大, 首次加载缓慢

vue2项目打包后js文件过大, 首次加载缓慢 安装插件 npm i compression-webpack-plugin6.1.1 -D配置vue.config.js const CompressionWebpackPlugin require(compression-webpack-plugin)module.exports {configureWebpack: {plugins:[new CompressionWebpackPlugin({filen…

Deepseek引爆AI热潮 防静电地板如何守护数据中心安全

近期&#xff0c;Deepseek的爆火将人工智能推向了新的高度&#xff0c;也引发了人们对AI背后基础设施的关注。作为AI运行的“大脑”&#xff0c;数据中心承载着海量数据的存储、处理和传输&#xff0c;其安全稳定运行至关重要。而在这背后&#xff0c;防静电地板扮演着不可或缺…

Arcgis 实用制图技巧--如何制作“阴影”效果

今天给大家介绍arcgis中阴影效果的制作方法,操作很简单,在ArcMap当中使用制图表达和移动几何方式就可以轻松实现。 左侧地图的图形背景组织很差。右侧地图通过使用阴影效果突出了重点内容。今天,我将要介绍两种阴影效果的创建方法:第一,纯色阴影(single color);第二,渐变…

Linux之kernel(4)netlink通信

Linux内核(04)之netlink通信 Author: Once Day Date: 2023年1月3日 一位热衷于Linux学习和开发的菜鸟&#xff0c;试图谱写一场冒险之旅&#xff0c;也许终点只是一场白日梦… 漫漫长路&#xff0c;有人对你微笑过嘛… 全系列文章可查看专栏: Linux内核知识_Once-Day的博客-…

FPGA开发时序图绘制

开始的时候画时序图都是拿 visio 硬连&#xff0c;但是那个线宽太难统一了&#xff0c;丑不拉几的&#xff0c;遂学习 waveform 语法使用代码来画时序图。 开始 Vscode 中安装 waveform render 或者在 GitHub 搜索 wavedrom 安装即可。由于 vscode 是我常用的编辑器&#xff…

jmeter聚合报告如何添加单位_性能测试连载(8)jmeter压力测试中的难点解析

概述 新人在用jmeter做压力测试的时候&#xff0c;会被一些性能术语搞懵&#xff0c;直接导致的后果就是对测试出来的结果数据根本不能理解&#xff0c;更谈不上分析。这篇文章着重给大家实例解释一下jmeter压力测试的一些专有名词 问题1&#xff1a;什么是压力测试 问到如何做…