找到第一天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;
}