EX_25/2/22

news/2025/2/25 11:58:11

找到第一天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.niftyadmin.cn/n/5865468.html

相关文章

JMeter性能问题

性能测试中TPS上不去的几种原因 性能测试中TPS上不去的几种原因_tps一直上不去-CSDN博客 网络带宽 连接池 垃圾回收机制 压测脚本 通信连接机制 数据库配置 硬件资源 压测机 业务逻辑 系统架构 CPU过高什么原因 性能问题分析-CPU偏高 - 西瓜汁拌面 - 博客园 US C…

选择排序:简单高效的选择

大家好&#xff0c;今天我们来聊聊选择排序&#xff08;Selection Sort&#xff09;算法。这是一个非常简单的排序算法&#xff0c;适合用来学习排序的基本思路和操作。选择排序在许多排序算法中以其直观和易于实现的特点著称&#xff0c;虽然它的效率不如其他高效算法&#xf…

在arm64设备(树莓派4B)上部署Hyperledger Fabric V2.5

arm64设备相较于x86设备功耗低、硬件成本低,树莓派4B是一个流行的arm64设备,本文记录在树莓派4B中部署Hyperledger Fabric V2.5的关键点,即与x86架构启动网络所需材料的差异。 成功部署示例 如图所示,Hyperledger Fabric V2.5.6 成功运行在了树莓派4B(8G内存版)中,即te…

奇异值分解(SVD)拟合平面

奇异值分解&#xff08;SVD&#xff09;拟合平面 在三维空间中&#xff0c;使用奇异值分解&#xff08;SVD&#xff09;来拟合平面是一种常见且有效的方法。下面将详细介绍其原理、实现步骤&#xff0c;并给出Python代码示例。 原理 给定一组三维空间中的点 P { p 1 , p 2…

【无标题】PHP-get_definde_vars

[题目信息]&#xff1a; 题目名称题目难度PHP-get_defined_vars2 [题目考点]&#xff1a; get_defined_vars — 返回由所有已定义变量所组成的数组此函数返回一个包含所有已定义变量列表的多维数组&#xff0c;这些变量包括环境变量、服务器变量和用户定义的变量。 [Flag格式…

DeepSeek-R1:通过强化学习激发大语言模型的推理能力

注&#xff1a;此文章内容均节选自充电了么创始人&#xff0c;CEO兼CTO陈敬雷老师的新书《自然语言处理原理与实战》&#xff08;人工智能科学与技术丛书&#xff09;【陈敬雷编著】【清华大学出版社】 文章目录 DeepSeek大模型技术系列三DeepSeek大模型技术系列三》DeepSeek-…

如何解决 MySQL 数据库服务器 CPU 飙升的情况

大家好&#xff0c;我是 V 哥。当 MySQL 数据库服务器 CPU 飙升时&#xff0c;我们应该怎么办&#xff1f;从何入手解决问题&#xff0c;有没有什么套路&#xff0c;因为自古真情留不住&#xff0c;唯有套路得人心&#xff0c;虽然这是一句玩笑话&#xff0c;也算很贴切&#x…

【算法与数据结构】Dijkstra算法求单源最短路径问题

目录 Dijkstra算法 算法简介&#xff1a; 该算法的核心思想&#xff1a; 算法特点&#xff1a; 算法示例演示&#xff1a; 算法实现&#xff1a; 邻接矩阵存图 邻接表存图&#xff1a; 时间复杂度分析&#xff1a; Dijkstra算法 算法简介&#xff1a; Dijkstra算法&am…